C/C++ 獲取文件夾下所有文件名 windows和linux通用

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


利用C/C++編寫程序以獲取文件夾內所有子文件名,以下程序參考網絡上諸多博文:

頭文件如下:

  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;  

程序如下:

  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環境:

  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環境:

  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環境下頭文件:

  1. #include <direct.h>  

linux環境下頭文件:

  1. #include <unistd.h>  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章