How to Iterate Through Every File/Directory Recursively in Standard C++

How do you iterate through every file/directory recursively in standard C++?

In standard C++, technically there is no way to do this since standard C++ has no conception of directories. If you want to expand your net a little bit, you might like to look at using Boost.FileSystem. This has been accepted for inclusion in TR2, so this gives you the best chance of keeping your implementation as close as possible to the standard.

An example, taken straight from the website:

bool find_file( const path & dir_path,         // in this directory,
const std::string & file_name, // search for this name,
path & path_found ) // placing path here if found
{
if ( !exists( dir_path ) ) return false;
directory_iterator end_itr; // default construction yields past-the-end
for ( directory_iterator itr( dir_path );
itr != end_itr;
++itr )
{
if ( is_directory(itr->status()) )
{
if ( find_file( itr->path(), file_name, path_found ) ) return true;
}
else if ( itr->leaf() == file_name ) // see below
{
path_found = itr->path();
return true;
}
}
return false;
}

How can I get the list of files in a directory using C or C++?

UPDATE 2017:

In C++17 there is now an official way to list files of your file system: std::filesystem. There is an excellent answer from Shreevardhan below with this source code:

#include <string>
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;

int main()
{
std::string path = "/path/to/directory";
for (const auto & entry : fs::directory_iterator(path))
std::cout << entry.path() << std::endl;
}

Old Answer:

In small and simple tasks I do not use boost, I use dirent.h. It is available as a standard header in UNIX, and also available for Windows via a compatibility layer created by Toni Ronkko.

DIR *dir;
struct dirent *ent;
if ((dir = opendir ("c:\\src\\")) != NULL) {
/* print all the files and directories within directory */
while ((ent = readdir (dir)) != NULL) {
printf ("%s\n", ent->d_name);
}
closedir (dir);
} else {
/* could not open directory */
perror ("");
return EXIT_FAILURE;
}

It is just a small header file and does most of the simple stuff you need without using a big template-based approach like boost (no offence, I like boost!).

Recursively iterating through directory using QDirIterator

Is there a way to make QDirIterator "refresh" or is there a better way
to do what I am trying to accomplish?

The problem is that with the scan filling the iterator object with directories and files structure you cannot get new file system changes. But you can definitely find help in Qt. It is called QFileSystemWatcher. To implement the functionality like you want I would first scan for directories and add them (or maybe just a root, depends) to watch. The simple example for watching just a directory: How to use QFileSystemWatcher to monitor a folder for change

How to recursively walk through a directory and print all files in C?

Here is a recursive code that does that:

void sprint(char *filename, char * dirToOpen, int level = 0)
{
DIR *dir;
struct dirent *entry;
struct stat s;

if (!(dir = opendir(dirToOpen)))
return;
if (!(entry = readdir(dir)))
return;

do
{
if(lstat(dirToOpen, &s) == 0 && S_ISDIR(s.st_mode)) /*if it's a directory*/
{
char path[1024];
int len = snprintf(path, sizeof(path)-1, "%s/%s", dirToOpen, entry->d_name); /*makes pathname*/
path[len] = 0;
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) /*if the directory isn't . or ..*/
continue;
printf("%*s[%s]\n", level * 2, "", entry->d_name);
sprint(filename ,path, level + 1);
}
else
{
if(strcmp(entry->d_name, filename) == 0 || strcmp(filename, ".") == 0) /*if entry name corresponds to filename, print it*/
printf("%*s- %s\n", 2, "", entry->d_name);
}
} while (entry = readdir(dir)); /*while there are more entries to read*/
closedir(dir);
}

Call it with sprint(".", "."); to recursively walk through a directory and print out all of the files.

Inspired from this answer.

How do you iterate through every file/directory recursively in standard C++?

In standard C++, technically there is no way to do this since standard C++ has no conception of directories. If you want to expand your net a little bit, you might like to look at using Boost.FileSystem. This has been accepted for inclusion in TR2, so this gives you the best chance of keeping your implementation as close as possible to the standard.

An example, taken straight from the website:

bool find_file( const path & dir_path,         // in this directory,
const std::string & file_name, // search for this name,
path & path_found ) // placing path here if found
{
if ( !exists( dir_path ) ) return false;
directory_iterator end_itr; // default construction yields past-the-end
for ( directory_iterator itr( dir_path );
itr != end_itr;
++itr )
{
if ( is_directory(itr->status()) )
{
if ( find_file( itr->path(), file_name, path_found ) ) return true;
}
else if ( itr->leaf() == file_name ) // see below
{
path_found = itr->path();
return true;
}
}
return false;
}

Recursive folder read C++

Unfortunately, best available thing is boost::filesystem::recursive_directory_iterator or, in case of fresh compiler - std::experimental::filesystem::recursive_directory_iterator

Examples are available in links provided



Related Topics



Leave a reply



Submit