递归获取文件夹中的所有文件的完整路径,传入的路径格式(相对路径或者绝对路径)如:C:\files
#ifdef WIN32
#include <io.h>
#include <iostream>
#include <vector>
using namespace std;
void getFiles(string path, vector<string>& files)
{
//文件句柄
long hFile = 0;
//文件信息,声明一个存储文件信息的结构体
struct _finddata_t fileinfo;
string p;//字符串,存放路径
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函数结束查找
_findclose(hFile);
}
}
#else
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
void getFiles(std::string path, std::vector<std::string>& files)
{
DIR *dir;
struct dirent *ptr;
if ((dir = opendir(path.c_str())) == NULL)
{
perror("Open dir error...");
return;
}
while ((ptr = readdir(dir)) != NULL)
{
if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0) ///current dir OR parrent dir
continue;
else if (ptr->d_type == 8) ///file
{
std::string strFile;
strFile = path;
strFile += "/";
strFile += ptr->d_name;
files.push_back(strFile);
}
else
{
continue;
}
}
closedir(dir);
return;
}
#endif