linux c++ 遍历文件夹

合理使用man,如:

man opendir
#include <iostream>
#include <string>
#include <string.h>
#include <dirent.h>

using namespace std;

string folder_path = "/home/amazing/";

int main()
{
    DIR *dirp;
    struct dirent *dirEntry;
    dirp = opendir(folder_path.c_str());
    if(dirp==NULL)
    {
        printf("%s is not a folder!\n", folder_path.c_str());
    }
    else
    {
        while((dirEntry = readdir(dirp)) !=NULL)
        {
            printf("%s %d\n", dirEntry->d_name, dirEntry->d_type);
        }
    }
    closedir(dirp);
    return 0;
}
文章目录