文件路径及其文件名内容获取

本文介绍了一种在Windows和Linux平台上实现遍历指定文件夹及其子文件夹内所有文件的方法,并提供了C++代码示例,重点展示了如何筛选特定格式如.jpg的文件。

由于经常有读取一个文件夹中的很多随机编号的文件,很多时候需要读取某些特定格式的所有文件。

下面的代码可以读取指定文件家中的所有文件和文件夹中格式为jpg的文件

参考:http://www.2cto.com/kf/201407/316515.html

windows平台代码:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include <io.h>  
  2. #include <fstream>  
  3. #include <string>  
  4. #include <vector>  
  5. #include <iostream>  
  6.  using namespace std;  
  7.   
  8.   
  9. //获取所有的文件名  
  10. void GetAllFiles( string path, vector<string>& files)    
  11. {    
  12.   
  13.     long   hFile   =   0;    
  14.     //文件信息    
  15.     struct _finddata_t fileinfo;    
  16.     string p;    
  17.     if((hFile = _findfirst(p.assign(path).append("\\*").c_str(),&fileinfo)) !=  -1)    
  18.     {    
  19.         do    
  20.         {     
  21.             if((fileinfo.attrib &  _A_SUBDIR))    
  22.             {    
  23.                 if(strcmp(fileinfo.name,".") != 0  &&  strcmp(fileinfo.name,"..") != 0)    
  24.                 {  
  25.                     files.push_back(p.assign(path).append("\\").append(fileinfo.name) );  
  26.                     GetAllFiles( p.assign(path).append("\\").append(fileinfo.name), files );   
  27.                 }  
  28.             }    
  29.             else    
  30.             {    
  31.                 files.push_back(p.assign(path).append("\\").append(fileinfo.name) );    
  32.             }   
  33.   
  34.         }while(_findnext(hFile, &fileinfo)  == 0);    
  35.   
  36.         _findclose(hFile);   
  37.     }   
  38.   
  39. }    
  40.   
  41. //获取特定格式的文件名  
  42. void GetAllFormatFiles( string path, vector<string>& files,string format)    
  43. {    
  44.     //文件句柄    
  45.     long   hFile   =   0;    
  46.     //文件信息    
  47.     struct _finddata_t fileinfo;    
  48.     string p;    
  49.     if((hFile = _findfirst(p.assign(path).append("\\*" + format).c_str(),&fileinfo)) !=  -1)    
  50.     {    
  51.         do    
  52.         {      
  53.             if((fileinfo.attrib &  _A_SUBDIR))    
  54.             {    
  55.                 if(strcmp(fileinfo.name,".") != 0  &&  strcmp(fileinfo.name,"..") != 0)    
  56.                 {  
  57.                     //files.push_back(p.assign(path).append("\\").append(fileinfo.name) );  
  58.                     GetAllFormatFiles( p.assign(path).append("\\").append(fileinfo.name), files,format);   
  59.                 }  
  60.             }    
  61.             else    
  62.             {    
  63.                 files.push_back(p.assign(path).append("\\").append(fileinfo.name) );    
  64.             }    
  65.         }while(_findnext(hFile, &fileinfo)  == 0);    
  66.   
  67.         _findclose(hFile);   
  68.     }   
  69. }   
  70.   
  71. // 该函数有两个参数,第一个为路径字符串(string类型,最好为绝对路径);  
  72. // 第二个参数为文件夹与文件名称存储变量(vector类型,引用传递)。  
  73. // 在主函数中调用格式(并将结果保存在文件"AllFiles.txt"中,第一行为总数):  
  74.   
  75. int main()  
  76. {  
  77.     string filePath = "testimages\\water";    
  78.     vector<string> files;    
  79.     char * distAll = "AllFiles.txt";  
  80.   
  81.     //读取所有的文件,包括子文件的文件  
  82.     //GetAllFiles(filePath, files);  
  83.   
  84.     //读取所有格式为jpg的文件  
  85.     string format = ".jpg";  
  86.     GetAllFormatFiles(filePath, files,format);  
  87.     ofstream ofn(distAll);  
  88.     int size = files.size();   
  89.     ofn<<size<<endl;  
  90.     for (int i = 0;i<size;i++)    
  91.     {    
  92.         ofn<<files[i]<<endl;   
  93.         cout<< files[i] << endl;  
  94.     }  
  95.     ofn.close();  
  96.     return 0;  
  97. }  

Linux平台代码:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //LINUX/UNIX c获取某个目录下的所有文件的文件名  
  2.    
  3. #include <stdio.h>  
  4. #include <dirent.h>  
  5. int main(int argc, char * argv[])  
  6. {  
  7.     struct dirent *ptr;      
  8.     DIR *dir;  
  9.     dir=opendir("./file");  
  10.     printf("文件列表:\n");  
  11.     while((ptr=readdir(dir))!=NULL)  
  12.     {  
  13.    
  14.         //跳过'.'和'..'两个目录  
  15.         if(ptr->d_name[0] == '.')  
  16.             continue;  
  17.         printf("%s\n",ptr->d_name);  
  18.     }  
  19.     closedir(dir);  
  20.     return 0;  
  21. }  
原文链接:http://blog.youkuaiyun.com/adong76/article/details/39432467

C++ 版本

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include <iostream>  
  2. #include <vector>  
  3. #include <string>  
  4. #include <dirent.h>  
  5. using namespace std;  
  6.   
  7. int main(int argc, char * argv[])  
  8. {  
  9.     struct dirent *ptr;      
  10.     DIR *dir;  
  11.     string PATH = "./file";  
  12.     dir=opendir(PATH.c_str());   
  13.     vector<string> files;  
  14.     cout << "文件列表: "<< endl;  
  15.     while((ptr=readdir(dir))!=NULL)  
  16.     {  
  17.    
  18.         //跳过'.'和'..'两个目录  
  19.         if(ptr->d_name[0] == '.')  
  20.             continue;  
  21.         //cout << ptr->d_name << endl;  
  22.         files.push_back(ptr->d_name);  
  23.     }  
  24.       
  25.     for (int i = 0; i < files.size(); ++i)  
  26.     {  
  27.         cout << files[i] << endl;  
  28.     }  
  29.   
  30.     closedir(dir);  
  31.     return 0;  
  32. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值