print all file names in directory cpp 1

print all file names in directory cpp

#include 
#include 
#include 
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;
}

Here is what the above code is Doing:
1. We include the filesystem header.
2. We create a namespace alias for the filesystem namespace.
3. We create a string variable called path and assign it the path to the directory we want to iterate over.
4. We create a for loop that iterates over the directory_iterator object.
5. We print the path of each entry in the directory.

Similar Posts