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
Is It Safe to Delete a Null Pointer
To_String Is Not a Member of Std, Says G++ (Mingw)
Initializer_List and Move Semantics
How to Redirect Cin and Cout to Files
Enum to String in Modern C++11/C++14/C++17 and Future C++20
How to Remove Unused C/C++ Symbols With Gcc and Ld
Finding C++ Static Initialization Order Problems
C++0X Has No Semaphores? How to Synchronize Threads
Significance of -Pthread Flag When Compiling
Why Is There an Injected Class Name
Converting a Hex String to a Byte Array
Copy a File in a Sane, Safe and Efficient Way
Is #Pragma Once a Safe Include Guard
Static Linking VS Dynamic Linking
When Should I Really Use Noexcept
Why Are Standard Iterator Ranges [Begin, End) Instead of [Begin, End]