V
V
Vadim Ushakov2022-01-08 07:26:51
C++ / C#
Vadim Ushakov, 2022-01-08 07:26:51

Recursive enumeration of files, how?

Why does the recursive enumeration of files on the C:\\ drive break and how to write a high-quality recursive enumeration of files?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
rPman, 2022-01-08
@rPman

Most likely, enumeration of files means getting a list of all files on the disk
. Recursively, it means you need to write a method that should enumerate the list of files and directories in the specified directory, and for each directory found, call itself with this directory in the parameters.
Be careful, you need to check for cyclic symbolic links (there are such in user profiles), otherwise the recursion will loop, the stack will overflow and the program will crash. You also need to check for errors, many directories are inaccessible due to access rights settings.

W
wally, 2022-01-08
@Yum1

You can use the solution of a recursive iterator over directories and files from the STL:
https://en.cppreference.com/w/cpp/filesystem/recur...
Or an analog from Boost:
https://www.boost.org/doc/libs /1_55_0/libs/filesys...
Sample code:

#include <fstream>
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
 
int main()
{
    fs::current_path(fs::temp_directory_path());
    fs::create_directories("sandbox/a/b");
    std::ofstream("sandbox/file1.txt");
    fs::create_symlink("a", "sandbox/syma");
    for(auto& p: fs::recursive_directory_iterator("sandbox"))
        std::cout << p.path() << '\n';
    fs::remove_all("sandbox");
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question