16,471
社区成员
发帖
与我相关
我的任务
分享
#include <string>
#include <io.h>
#include <vector>
#include <iostream>
using namespace std ;
void getFiles( string path, string exd, vector<string>& files )
{
//文件句柄
long hFile = 0;
//文件信息
struct _finddata_t fileinfo;
string pathName, exdName;
if (0 != strcmp(exd.c_str(), ""))
{
exdName = "\\*." + exd;
}
else
{
exdName = "\\*";
}
if((hFile = _findfirst(pathName.assign(path).append(exdName).c_str(),&fileinfo)) != -1)
{
do
{
//如果是文件夹中仍有文件夹,迭代之
//如果不是,加入列表
if((fileinfo.attrib & _A_SUBDIR))
{
if(strcmp(fileinfo.name,".") != 0 && strcmp(fileinfo.name,"..") != 0)
getFiles( pathName.assign(path).append("\\").append(fileinfo.name), exd, files );
}
else
{
if(strcmp(fileinfo.name,".") != 0 && strcmp(fileinfo.name,"..") != 0)
files.push_back(pathName.assign(path).append("\\").append(fileinfo.name));
}
}while(_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
}
int main()
{
string filePath = "D:\\";
vector<string> files;
//获取该路径下的所有jpg文件
getFiles(filePath, "jpg", files);
int size = files.size();
for (int i = 0;i < size;i++)
{
cout<<files[i].c_str()<<endl;
}
return 0;
}
#include <string>
#include <io.h>
#include <vector>
#include <iostream>
using namespace std;
void getFiles( string path, string ext, vector<string> &files )
{
string find(path);
if (ext.size() > 0)
{
find += "\\*." + ext;
}
else
{
find += "\\*";
}
struct _finddata_t fileinfo;
intptr_t hf = _findfirst(find.c_str(),&fileinfo);
if (hf != -1)
{
do {
if ((fileinfo.name == ".") || (fileinfo.name == ".."))
{
continue;
}
string file(path);
file += "\\" + fileinfo.name;
if((fileinfo.attrib & _A_SUBDIR))
{
getFiles(file, ext, files);
}
else
{
files.push_back(file);
}
} while (_findnext(hf, &fileinfo) == 0);
_findclose(hf);
}
}
int main()
{
string filePath = "D:\\";
vector<string> files;
//获取该路径下的所有jpg文件
getFiles(filePath, "jpg", files);
int size = files.size();
for (int i = 0;i < size;i++)
{
cout<<files[i].c_str()<<endl;
}
return 0;
}