C/C++ 获取文件夹下所有文件名 windows和linux通用

#################################################


利用C/C++编写程序以获取文件夹内所有子文件名,以下程序参考网络上诸多博文:

头文件如下:

[cpp]  view plain  copy
  1. #include <iostream>  
  2. #include <stdlib.h>  
  3. #include <stdio.h>  
  4. #include <string.h>  
  5. #ifdef linux  
  6. #include <unistd.h>  
  7. #include <dirent.h>  
  8. #endif  
  9. #ifdef WIN32  
  10. #include <direct.h>  
  11. #include <io.h>  
  12. #endif  
  13. using namespace std;  

程序如下:

[cpp]  view plain  copy
  1. <pre name="code" class="cpp">/** 
  2.  * @function: 获取cate_dir目录下的所有文件名 
  3.  * @param: cate_dir - string类型 
  4.  * @result:vector<string>类型 
  5. */  
  6. vector<string> getFiles(string cate_dir)  
  7. {  
  8.     vector<string> files;//存放文件名  
  9.   
  10. #ifdef WIN32  
  11.     _finddata_t file;  
  12.     long lf;  
  13.     //输入文件夹路径  
  14.     if ((lf=_findfirst(cate_dir.c_str(), &file)) == -1) {  
  15.         cout<<cate_dir<<" not found!!!"<<endl;  
  16.     } else {  
  17.         while(_findnext(lf, &file) == 0) {  
  18.             //输出文件名  
  19.             //cout<<file.name<<endl;  
  20.             if (strcmp(file.name, ".") == 0 || strcmp(file.name, "..") == 0)  
  21.                 continue;  
  22.             files.push_back(file.name);  
  23.         }  
  24.     }  
  25.     _findclose(lf);  
  26. #endif  
  27.   
  28. #ifdef linux  
  29.     DIR *dir;  
  30.     struct dirent *ptr;  
  31.     char base[1000];  
  32.    
  33.     if ((dir=opendir(cate_dir.c_str())) == NULL)  
  34.         {  
  35.         perror("Open dir error...");  
  36.                 exit(1);  
  37.         }  
  38.    
  39.     while ((ptr=readdir(dir)) != NULL)  
  40.     {  
  41.         if(strcmp(ptr->d_name,".")==0 || strcmp(ptr->d_name,"..")==0)    ///current dir OR parrent dir  
  42.                 continue;  
  43.         else if(ptr->d_type == 8)    ///file  
  44.             //printf("d_name:%s/%s\n",basePath,ptr->d_name);  
  45.             files.push_back(ptr->d_name);  
  46.         else if(ptr->d_type == 10)    ///link file  
  47.             //printf("d_name:%s/%s\n",basePath,ptr->d_name);  
  48.             continue;  
  49.         else if(ptr->d_type == 4)    ///dir  
  50.         {  
  51.             files.push_back(ptr->d_name);  
  52.             /* 
  53.                 memset(base,'\0',sizeof(base)); 
  54.                 strcpy(base,basePath); 
  55.                 strcat(base,"/"); 
  56.                 strcat(base,ptr->d_nSame); 
  57.                 readFileList(base); 
  58.             */  
  59.         }  
  60.     }  
  61.     closedir(dir);  
  62. #endif  
  63.   
  64.     //排序,按从小到大排序  
  65.     sort(files.begin(), files.end());  
  66.     return files;  
  67. }  


windows环境下需要加上cate_dir+"\\*"


实现:获取当前目录下的文件名:

windows环境:

[cpp]  view plain  copy
  1. int main(void)  
  2. {  
  3.     char current_address[100];  
  4.     memset(current_address, 0, 100);  
  5.     getcwd(current_address, 100); //获取当前路径  
  6.     cout<<current_address<<endl;  
  7.     strcat(current_address, "\\*");  
  8.   
  9.     vector<string> files=getFiles((string)current_address);  
  10.     for (int i=0; i<files.size(); i++)  
  11.     {  
  12.         cout<<files[i]<<endl;  
  13.     }  
  14.   
  15.     //cout<<"Hello World"<<endl;  
  16.   
  17.     cout<<"end..."<<endl;  
  18.     cin.get();  
  19.     return 0;  
  20. }  

linux环境:

[cpp]  view plain  copy
  1. int main(void)  
  2. {  
  3.     DIR *dir;  
  4.     char basePath[100];  
  5.   
  6.     ///get the current absoulte path  
  7.     memset(basePath, '\0'sizeof(basePath));  
  8.     getcwd(basePath, 999);  
  9.     printf("the current dir is : %s\n",basePath);  
  10.   
  11.        
  12.     cout<<endl<<endl;  
  13.     vector<string> files=getFiles(basePath);  
  14.     for (int i=0; i<files.size(); i++)  
  15.     {  
  16.         cout<<files[i]<<endl;  
  17.     }  
  18.   
  19.   
  20.     cout<<"end..."<<endl<<endl;  
  21.     return 0;  
  22. }  

###############################

参考:

http://blog.csdn.net/cscmaker/article/details/7042718

http://baike.baidu.com/item/getcwd


获取当前绝对路径getcwd():

windows环境下头文件:

[cpp]  view plain  copy
  1. #include <direct.h>  

linux环境下头文件:

[cpp]  view plain  copy
  1. #include <unistd.h>  

猜你喜欢

转载自blog.csdn.net/iot_shun/article/details/80562866