#include <iostream>
#include <memory.h>
#include <dirent.h>
#include <vector>
using namespace std;
string gFileLoadPath = "/home/mypath/";
vector<string> getFilesList(string dir);
int main()
{
char dir[200];
string Path;
cout << "Enter a directory: ";
cin.getline(dir, 200);
vector<string>allFileName = getFilesList(dir);
cout << "输出所有文件的路径:" << endl;
for (size_t i = 0; i < allFileName.size(); i++)
{
string filename = allFileName.at(i);
Path = filename;
cout << Path << endl;
}
return 0;
}
vector<string> getFilesList(string dirpath){
DIR *dir = opendir(dirpath.c_str());
if (dir == NULL)
{
cout << "opendir error" << endl;
}
vector<string> allPath;
struct dirent *entry;
while ((entry = readdir(dir)) != NULL)
{
if (entry->d_type == DT_DIR){//d_type:4(DT_DIR)表示为目录,8(DT_REG)表示为文件
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
string dirNew = dirpath + "/" + entry->d_name;
vector<string> tempPath = getFilesList(dirNew);//递归实现
allPath.insert(allPath.end(), tempPath.begin(), tempPath.end());
}else {
//cout << "name = " << entry->d_name << ", len = " << entry->d_reclen << ", entry->d_type = " << (int)entry->d_type << endl;
string name = entry->d_name;
string imgdir = dirpath +"/"+ name;
//sprintf("%s",imgdir.c_str());
allPath.push_back(imgdir);
}
}
closedir(dir);
//system("pause");
return allPath;
}
linux下遍历指定路径下文件,包含子文件夹中的文件
猜你喜欢
转载自blog.csdn.net/qq_25244255/article/details/86635933
今日推荐
周排行