原文:http://blog.csdn.net/cxf7394373/article/details/7195661
1 #include<iostream> 2 #include<fstream> 3 #include<vector> 4 #include<string.h> 5 #include<io.h> 6 using namespace std; 7 void getfiles(string filepath, vector<string> &files); 8 9 int main() { 10 fstream file; 11 vector<string> files; 12 string filepath = "D:\学习资料"; 13 getfiles(filepath, files); 14 int size = files.size(); 15 for (int i = 0; i < size; i++) { 16 cout << files[i].c_str() << endl; 17 } 18 return 0; 19 } 20 21 void getfiles(string path, vector<string>& files) { 22 //文件句柄 23 long hFile = 0; 24 //文件信息 25 struct _finddata_t fileinfo; 26 string p; 27 if ((hFile = _findfirst(p.assign(path).append("\*").c_str(), &fileinfo)) 28 != -1) { 29 do { 30 //如果是目录,迭代之 31 //如果不是,加入列表 32 if ((fileinfo.attrib & _A_SUBDIR)) { 33 if (strcmp(fileinfo.name, ".") != 0 34 && strcmp(fileinfo.name, "..") != 0) 35 getfiles(p.assign(path).append("\").append(fileinfo.name), 36 files); 37 } else { 38 files.push_back( 39 p.assign(path).append("\").append(fileinfo.name)); 40 } 41 } while (_findnext(hFile, &fileinfo) == 0); 42 _findclose(hFile); 43 } 44 }