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

前端 未结 27 3270
情书的邮戳
情书的邮戳 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:12

    This worked for me. It writes a file with just the names (no path) of all the files. Then it reads that txt file and prints it for you.

    void DisplayFolderContent()
        {
    
            system("dir /n /b * > file_names.txt");
            char ch;
            std::fstream myStream("file_names.txt", std::fstream::in);
            while (myStream.get(ch))
            {
                std::cout << ch;
            }
    
        }
    

提交回复
热议问题