How can I get the list of files in a directory using C or C++?
In the above answered question, there was sample code for C++ iterating through all the files of a single folder, which looked like this:
#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;
}
but when I tried incorporating it into my program where I can already successfully open one single .jpg file or .png file at a time (yay!), I got the following 2 problem from the Visual Studio 2019 C++ compiler:
namespace "std" has no member "filesystem"
name followed by '::' must be a class or namespace name
And I guess, it's a simple syntax error in VS2019 C++, related to namespace but I don't know how to fix it. If I understand C++ (and it has been a few years), namespace declarations are a shortcut to concise code but still being sure the compiler uses the correct class when there are over(what was the term) of the same name from like different classes? In other words,
namespace fs = std::filesystem;
will replace all instances of fs with std::filesystem to allow cleaner code. I can get rid of the first error by replace fs with std::filesystem
for (const auto & entry : std::filesystem::directory_iterator(path))
but the second error remains, so I still don't understand this error. Is it not finding filesystem from the standard library, and do you have to manually do something in VS2019 C++ to be able to #include ?