C++讀取特定路徑下文件目錄及文件名稱


 

這幾天在弄一個項目,需要讀取給定路徑下的所有文件夾名稱或所有帶后綴的文件名。查了下C++的資料,發現有很多方法,這里總結其中的一種,其他用法類似。主要用到了以下幾個頭文件(類):io.h,fstream,string。

1、讀取某給定路徑下所有文件夾與文件名稱,並帶完整路徑。代碼如下:

 1 void getAllFiles(string path, vector<string>& files) {
 2     //文件句柄
 3     long hFile = 0;
 4     //文件信息
 5     struct _finddata_t fileinfo;  //很少用的文件信息讀取結構
 6     string p;  //string類很有意思的一個賦值函數:assign(),有很多重載版本
 7     if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(),&fileinfo)) != -1) {
 8         do {
 9             if ((fileinfo.attrib & _A_SUBDIR)) {  //比較文件類型是否是文件夾
10                 if (strcmp(fileinfo.name,".") != 0 && strcmp(fileinfo.name,"..") != 0) {
11                     files.push_back(p.assign(path).append("\\").append(fileinfo.name));
12                     getAllFiles(p.assign(path).append("\\").append(fileinfo.name), files);
13                 }
14             } else {
15                 files.push_back(p.assign(path).append("\\").append(fileinfo.name));
16             }
17         } while (_findnext(hFile, &fileinfo) == 0);  //尋找下一個,成功返回0,否則-1
18         _findclose(hFile);
19     }
20 }

該函數有兩個參數,第一個為路徑字符串(string類型,最好為絕對路徑);第二個參數為文件夾與文件名稱存儲變量(vector類型,引用傳遞)。在主函數中調用格式(並將結果保存在文件"AllFiles.txt"中,第一行為總數):

 1 char* filePath = "E:\\YunShi";
 2 vector<string> files;
 3 char* distAll = "AllFiles.txt";
 4 getAllFiles(filePath, files);
 5 ofstream ofn(distAll);
 6 int size = files.size();
 7 ofn<<size<<endl;
 8 for (int i = 0; i < size; i++) {
 9     ofn<<files[i]<<endl;
10 }
11 ofn.close();

結果如下圖:

同理,只讀取某給定路徑下的當前文件夾名(以下類似,只給出函數,調用案例同上):

 1 void getJustCurrentDir(string path, vector<string>& files) {
 2     //文件句柄
 3     long hFile = 0;
 4     //文件信息 
 5     struct _finddata_t fileinfo;
 6     string p;
 7     if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(),&fileinfo)) != -1) {
 8         do {  
 9             if ((fileinfo.attrib & _A_SUBDIR)) {  
10                 if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0) {
11                     files.push_back(fileinfo.name);
12                     //files.push_back(p.assign(path).append("\\").append(fileinfo.name));
13                 }
14             }
15         } while (_findnext(hFile, &fileinfo) == 0);
16         _findclose(hFile);
17     }
18 }

只讀取某給定路徑下的當前文件名:

 1 void getJustCurrentFile(string path, vector<string>& files) {
 2     //文件句柄
 3     long hFile = 0;
 4     //文件信息
 5     struct _finddata_t fileinfo;
 6     string p;
 7     if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1) {
 8         do {
 9             if ((fileinfo.attrib & _A_SUBDIR)) {
10                 ;
11             } else {
12                 files.push_back(fileinfo.name);
13                 //files.push_back(p.assign(path).append("\\").append(fileinfo.name));
14             }
15         } while (_findnext(hFile, &fileinfo) == 0);
16         _findclose(hFile);
17     }
18 }

只讀取某給定路徑下的所有文件名(即包含當前目錄及子目錄的文件):

 1 void getFilesAll(string path, vector<string>& files) {
 2     //文件句柄
 3     long hFile = 0;
 4     //文件信息
 5     struct _finddata_t fileinfo;
 6     string p;  
 7     if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1) {
 8         do {
 9             if ((fileinfo.attrib & _A_SUBDIR)) {
10                 if (strcmp(fileinfo.name,".") != 0  &&  strcmp(fileinfo.name,"..") != 0) {
11                     //files.push_back(p.assign(path).append("\\").append(fileinfo.name));
12                     getFilesAll(p.assign(path).append("\\").append(fileinfo.name), files);
13                 }
14             } else {  
15                 files.push_back(p.assign(path).append("\\").append(fileinfo.name));
16             }
17         } while (_findnext(hFile, &fileinfo) == 0);
18         _findclose(hFile);
19     }
20 }

至此,簡單的讀取功能就實現了,以后只要指定路徑,就能輕松地得到對應的路徑與文件名稱。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM