1:
#path:要遍历的文件夹路径
#filenames:返回的所有文件路径
#include<sys/types.h>
#include<dirent.h>
void GetFileNames(string path, vector<string>& filenames)
{
DIR *pDir;
struct dirent* ptr;
if (!(pDir = opendir(path.c_str())))
return;
//cout << "file:" << ptr << endl;
//cout << "file:" << readdir(pDir) << endl;
while ((ptr = readdir(pDir)) != 0)
{
if (strcmp(ptr->d_name, ".") != 0 && strcmp(ptr->d_name, "..") != 0)
filenames.push_back(path + "//" + ptr->d_name);
}
closedir(pDir);
}
dirent.h头文件缺失问题: 以下链接下载即可:
https://github.com/tronkko/dirent
2:
#path:要遍历的文件夹路径
#files 返回的文件路径
#include<io.h>
void getFiles(string path, vector<string>& files)
{
intptr_t hFile = 0;//文件句柄,过会儿用来查找
struct _finddata_t fileinfo;//文件信息
string p;
//string pa = "C://Users//YAO//Documents//Visual Studio 2015//Projects//Project2//Project2//files//creat_feature_lib//points_filter";
cout << "ooo" << _findfirst(p.assign(path).append("//*").c_str(), &fileinfo) << endl;
if ((hFile = _findfirst(p.assign(path).append("//*").c_str(), &fileinfo)) != -1)
//如果查找到第一个文件
{
do
{
if ((fileinfo.attrib & _A_SUBDIR))//如果是文件夹
{
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
getFiles(p.assign(path).append("\\").append(fileinfo.name), files);
}
else//如果是文件
{
files.push_back(p.assign(path).append("\\").append(fileinfo.name));
}
} while (_findnext(hFile, &fileinfo) == 0); //能寻找到其他文件
_findclose(hFile); //结束查找,关闭句柄
}
}