Is There a Os Library for C++
Let's Say I Want to Make a File Manager, and I Need to Fetch the Names of All the Files in a Given Path. in Python I Would Do Something Like: Os...
Let's say I want to make a file manager, And I need to fetch the names of all the files in a given path. In python I would do something like: os.listdir(path). So is a c++ library like the OS module.
2 Answers
You can use
#include <filesystem>
for this.
For some older compilers you might need to check if only experimental filesystem is available and use that.
#pragma once
#if __has_include(<filesystem>)
#include <filesystem>
namespace filesystem = std::filesystem;
#else
#include <experimental/filesystem>
namespace filesystem = std::experimental::filesystem;
#endif
Then to iterate over files:
for (auto it : filesystem::directory_iterator("path/to/iterate"))
{
// Use it.path
}
// Or recursively
for (auto it : filesystem::recursive_directory_iterator("path/to/iterate"))
{
// Use it.path
}