1.引入头文件
#include <unistd.h>
#include <dirent.h>
#include <string>
2.函数
vector<string> getFiles(string cate_dir)
{
vector<string> files;//存放文件名
DIR *dir;
struct dirent *ptr;
if ((dir=opendir(cate_dir.c_str())) == nullptr)
{
perror("Open dir error...");
exit(1);
}
while ((ptr=readdir(dir)) != nullptr)
{
if(strcmp(ptr->d_name,".")==0 || strcmp(ptr->d_name,"..")==0) ///current dir OR parrent dir
continue;
if(ptr->d_type == 8) ///文件夹
{
files.push_back(cate_dir + "/" + ptr->d_name);
}
else if(ptr->d_type == 4) ///文件
{
vector<string> childfiles = getFiles(cate_dir + "/" + ptr->d_name);
files.insert(files.end(),childfiles.begin(),childfiles.end());
}
}
closedir(dir);
return files;
}