原博客迁移到:https://blog.csdn.net/u013171226/article/details/107680301
windows中,用C++遍历文件夹下的所有文件,得到包含绝对路径的文件列表。
#include <io.h> #include <Windows.h> #include <stdio.h> #include <string> #include <iostream> #include <direct.h> #include<vector> using namespace std; int FilesRead(string root, vector<string> &fileVec) { int Nums = 0; long long handle = 0; struct _finddata_t fileinfo; string temp_str; if ((handle = _findfirst(temp_str.assign(root).append("\*").c_str(), &fileinfo)) != -1) { do { if ((fileinfo.attrib&_A_SUBDIR)) { if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0) FilesRead(temp_str.assign(root).append(fileinfo.name).c_str(), fileVec); } else { try { if (fileinfo.size == 0) throw - 1; else fileVec.push_back(temp_str.assign(root).append("\").append(fileinfo.name)); } catch (int e) { if (e == -1) cout << "file is empty!" << endl; } } } while (_findnext(handle, &fileinfo) == 0); _findclose(handle); } Nums = (int)fileVec.size(); cout << "Nums: " << Nums << endl; if (Nums > 0) return Nums; else return 0; } int main() { std::string inPath = "E:\yidiandian\Surf";//遍历文件夹下的所有.jpg文件 vector<string> fileVec; FilesRead(inPath, fileVec); for (int i = 0; i < fileVec.size(); i++) { printf("name:%s ", fileVec[i].c_str()); } return 0; }