linux下遍历文件夹下的文件
零、前言
工作中经常用到遍历文件夹,下面的三个小函数可能会对您遍历文件夹提供一些思路,代码都是在linux下环境中测试通过的,Qt版和boost版理论上在windows也能编译通过,请自己测试使用。
一、linux C版
// 查找文件夹下的.XX格式的文件
list<string> getDirFile(const string dirPath,const char *extenStr)
{
list<string> filesNameList;
DIR *dir = opendir(dirPath.c_str());
dirent *pDirent = NULL;
while((pDirent = readdir(dir)) != NULL)
{
if(strstr(pDirent->d_name, extenStr) || strstr(pDirent->d_name, extenStr))
{
string pathFile = dirPath + string(pDirent->d_name);
filesNameList.push_back(pathFile);
}
}
closedir(dir);
return filesNameList;
}
二、boost版
#include <iostream>
#include <list>
#include <string>
#include <boost/filesystem.hpp>
#include <boost/f