读取文件夹下指定类型的文件(windows)

来源:互联网 发布:天天向上的网络作家 编辑:程序博客网 时间:2024/06/16 21:34

转自http://blog.csdn.net/carson2005/article/details/26452699

直接上代码

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include <iostream>  
  2. using namespace std;  
  3.   
  4. #ifdef WIN32  
  5. #include <io.h>  
  6. #else  
  7. #endif  
  8.   
  9. void ReadDirectory( const string& directoryName, const string fileExt, vector<string>& filenames, bool addDirectoryName=true )  
  10. {  
  11.     filenames.clear();  
  12.   
  13. #ifdef WIN32  
  14.     struct _finddata_t s_file;  
  15.     string str = directoryName + "\\*" + fileExt;  
  16.   
  17.     intptr_t h_file = _findfirst( str.c_str(), &s_file );  
  18.     if( h_file != static_cast<intptr_t>(-1.0) )  
  19.     {  
  20.         do  
  21.         {  
  22.             if( addDirectoryName )  
  23.                 filenames.push_back(directoryName + "\\" + s_file.name);  
  24.             else  
  25.                 filenames.push_back((string)s_file.name);  
  26.         }  
  27.         while( _findnext( h_file, &s_file ) == 0 );  
  28.     }  
  29.     _findclose( h_file );  
  30. #else  
  31.       
  32. #endif  
  33.   
  34.     sort( filenames.begin(), filenames.end() );  
  35. }  
  36.   
  37. int main()  
  38. {  
  39.     vector<string> fileNames;  
  40.     ReadDirectory("C:\\Users\\Administrator\\Desktop\\新建文件夹"".png", fileNames);  
  41.     for (int i=0; i<fileNames.size(); i++)  
  42.     {  
  43.         printf("%s \n", fileNames[i].c_str());  
  44.     }  
  45.     printf("ok \n");  
  46.   
  47.     system("pause");  
  48.     return 0;  
  49. }  

0 0
原创粉丝点击