Listing all files in the directories and subdirectories recursively in c++

56 views Asked by At

Why recursive_directory_iterator() is not listing all subdirectories recursively like it was said to be?

I have created solutions to c++ primer book where I have a main directory for the book which contains directories for each part and some files. Then each of those parts has directories for each chapters which contains solutions to all exercises.

I tried to list all files and directories recursively using both recursive_directory_iterator and directory_iterator in a recursive function separately but both listed same files and some directories in the main directory but all directories in the main.

For example, main has part_1, part_2, part_3, part_4, It lists only files in part_3 but not other part_*. I even tried passing follow_directory_symlink, but still unable to list all files. What I am doing wrong.

It even recursively shows all sub directories in Part_3.

#include <iostream>
#include <filesystem>

namespace fs = std::filesystem;

int main() {
    fs::path curr("/c++_primer_github");   //  I didn't post the original path
    for( const auto &entry : fs::recursive_directory_iterator(curr, fs::directory_options::follow_directory_symlink)) {
        if (fs::is_directory(entry)) {
            std::cout << "[+]" << entry.path().filename() << std::endl; 
        } else if (fs::is_regular_file(entry)) {
            std::cout << entry.path().filename() << " " << entry.file_size() << std::endl;
        }
    }
    return 0;
}
1

There are 1 answers

0
semicolon_missing On

Code works absolutely fine, the problem is since I was testing with the directory which contains my repository files, it also has a git folder with huge list of files created by git I believe. So as I was testing this code in vs code, it didn't show the entire output in the terminal, I can only scroll to certain extent, So the other subdirectories was not listed, ctrl+f directory name in the vs code terminal came with no results, so I was confused before but then I printed the output to a file. It contains the entire list of files and subdirectories as expected.