#include <iostream>
#include <vector>
#include <string>
#include <dirent.h>
using namespace std;
void SplitString(const std::string& s, std::vector<std::string>& v, const std::string& c)
{
std::string::size_type pos1, pos2;
pos2 = s.find(c);
pos1 = 0;
while(std::string::npos != pos2)
{
v.push_back(s.substr(pos1, pos2-pos1));
pos1 = pos2 + c.size();
pos2 = s.find(c, pos1);
}
if(pos1 != s.length())
v.push_back(s.substr(pos1));
}
void readFileList(string path, vector<string> &files, string ext){
struct dirent *ptr;
DIR *dir;
dir=opendir(path.c_str());
while((ptr=readdir(dir))!=NULL)
{
if(ptr->d_name[0] == '.')
continue;
std::vector<std::string> strVec;
SplitString(ptr->d_name, strVec, ".");
if(strVec.size() >=2)
{
string p;
if(strVec[strVec.size()-1].compare(ext) == 0)
files.push_back(p.assign(path).append("/").append(ptr->d_name));
}
else{
string p;
readFileList(p.assign(path).append("/").append(ptr->d_name), files, ext);
}
}
closedir(dir);
}
int main(int argc, char * argv[])
{
string PATH = "/home/mikeyna/图片";
vector<string> files;
readFileList(PATH, files, "png");
for (int i = 0; i < files.size(); ++i)
{
cout << files[i] << endl;
}
return 0;
}
C/C++ 获取文件夹下指定扩展名的文件列表
最新推荐文章于 2023-11-22 19:24:46 发布