How do I get a list of files in a directory in C++?

后端 未结 13 1398
南旧
南旧 2020-11-28 23:36

How do you get a list of files within a directory so each can be processed?

相关标签:
13条回答
  • 2020-11-29 00:05

    I've just asked a similar question and here's my solution based on answer received (using boost::filesystem library):

    #include <string>
    #include <iostream>
    #include <boost/filesystem.hpp>
    using namespace std;
    using namespace boost::filesystem;
    
    int main()
    {
        path p("D:/AnyFolder");
        for (auto i = directory_iterator(p); i != directory_iterator(); i++)
        {
            if (!is_directory(i->path())) //we eliminate directories in a list
            {
                cout << i->path().filename().string() << endl;
            }
            else
                continue;
        }
    }
    

    Output is like:

    file1.txt
    file2.dat
    
    0 讨论(0)
提交回复
热议问题