留一份网上存档,以后用得到!
#include <iostream> #include <string> #include <io.h> #include <vector> using namespace std; bool getDirFilesPath( string folderPath, vector<string>& files); int main() { string path; vector<string> files; cin >> path; getDirFilesPath(path,files); for (int i=0;i<files.size();i++) { cout<<files.at(i)<<"\n"; } system("pause"); return 0; } //获得某文件夹下的所有文件的绝对路径 bool getDirFilesPath( string folderPath, vector<string>& files) { //文件句柄 long hFile = 0; //文件信息 struct _finddata_t fileinfo; string p; if((hFile = _findfirst(p.assign(folderPath).append("\\*").c_str(),&fileinfo)) != -1) { do { //如果是目录,继续 if((fileinfo.attrib & _A_SUBDIR)) { if(strcmp(fileinfo.name,".") != 0 && strcmp(fileinfo.name,"..") != 0) { //cout<<"文件夹\n"; files.push_back( fileinfo.name ); } } else { files.push_back( fileinfo.name ); } }while(_findnext(hFile, &fileinfo) == 0); _findclose(hFile); }else{ return false; } return true; }
时间: 2024-09-19 15:41:52