下面是一种简便的读取文件夹里所有子文件夹和文件的方法,如果想读取子文件夹下所有文件,可以递归一下。
#include <dirent.h>
#include <stdio.h>
#include <string.h>
void read_dir(std::string fileDir, std::vector<std::string>& files)
{
struct dirent *ent = NULL;
DIR *dir = opendir(fileDir.c_str());
if (dir != NULL)
{
while ((ent = readdir(dir)) != NULL)
{
if (strcmp(ent->d_name, ".") != 0 && strcmp(ent->d_name, "..") != 0)
{
files.push_back(ent->d_name);
}
}
closedir(dir);
}
else
{
std::cout << "failed to open directory" << std::endl;
}
}
关于dirent.h找不到的问题,可以参考:https://blog.csdn.net/u013647759/article/details/94172076
参考:https://segmentfault.com/q/1010000006655732
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)