How can I get the list of files in a directory using C or C++?

前端 未结 27 3171
情书的邮戳
情书的邮戳 2020-11-21 05:30

How can I determine the list of files in a directory from inside my C or C++ code?

I\'m not allowed to execute the ls command and parse the results from

27条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-21 06:11

    Shreevardhan answer works great. But if you want to use it in c++14 just make a change namespace fs = experimental::filesystem;

    i.e.,

    #include 
    #include 
    #include 
    
    using namespace std;
    namespace fs = experimental::filesystem;
    
    int main()
    {
        string path = "C:\\splits\\";
        for (auto & p : fs::directory_iterator(path))
            cout << p << endl;
        int n;
        cin >> n;
    }
    

提交回复
热议问题