0

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 ?

  • Check project settings for both the working and the non-working project, compare the settings (especially compiler options). Are you really building using C++17 (the standard where `std::filesystem` were introduced) in the non-working project? – Some programmer dude Jul 26 '19 at 06:40
  • I think I figured it out, microsoft implements filesystem handlers in std::experimental::filesystem – Jon McGill Jul 26 '19 at 06:46
  • From your question I got the impression that you had successfully built the first code-snippet you show, in VS2019. If VS2019 only have `std::experimental::filesystem` that can't be true (that the program you show built and worked). In the future such details are quite critical to understanding the question and how to help you. Please take some time to read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Jul 26 '19 at 06:57
  • No, the first code snippet was some generally approved of method of doing what I want to do, and I even linked the question on stackoverflow and answer where it came from. I took a Loooot of time reading stuff before I asked this question. I still haven't figured it out, actually. I broke my code which had been working before, and I can't figure out where I broke it, so now I have to work backwards where I had it working earlier. – Jon McGill Jul 26 '19 at 07:09
  • And yes, I did have some code working that could open individual files, but I have not yet managed to do the folder thing. – Jon McGill Jul 26 '19 at 07:16
  • So, I got to the point where I can run the above code in VS2019 (yes using the std::experimental::filesystem), but in my Windows system this code snippet doesn't do anything (it did compile however), but I think the snippet must be a linux only implementation. Not sure. That's why I posted this question. Let me ask it this way: GetFileOpen(..) is a std api that works for opening files, but what is the windows api for opening a directory and iterating over its files somehow? – Jon McGill Jul 26 '19 at 18:55
  • 1
    Ah, maybe I didn't understand what you were saying about C++17, I checked the project properties and I guess maybe I was using C++14 (whatever the default was), I tried changing it to C++17, and then it compiles with std::filesystem, but the directory_iterator function call causes an exception which causes an error abort() message to pop up in a dialog box. – Jon McGill Jul 26 '19 at 19:12

0 Answers0