C ++フォルダートラバーサル関数

1 #include <iostream>
 2 #include <fstream>
 3 #include < string .h>
 4 #include <vector>
 5 #include <sys / io.h>
 6 #include <dirent.h>
 7 #include <stdlib。 h>
 8 #include <sys / stat.h>
 9 #include " tools.hpp " 
10  
11  // 遍历文件夹
12  void showAllFiles(const  char * dir_name、vector < string >&files)
 13  {
 14      // チェックパラメータ!
15     if(NULL == dir_name)
 16      {
 17          cout << " dir_name is null!" << endl;
18          リターン;
19      }
 20   
21      // dir_nameが有効なdir 
22      struct stat s かどうかを確認します。
23      lstat(dir_name、&s);
24      if(!S_ISDIR(s.st_mode))
 25      {
 26          cout << " dir_name is not a valid directory!" << endl;
27          帰り;
28      }
 29      
30      struct dirent * filename;    // readdir()の戻り値
31       DIR * dir;                   // opendir()の戻り値
32      dir = opendir(dir_name);
33      if(NULL == dir)
 34      {
 35          cout << " dirを開けません" << dir_name << endl;
36          リターン;
37      }
 38      cout << " dirのオープンに成功しました!" << endl;
39     
40      / * dir内のすべてのファイルを読み取る〜* / 
41      while((filename = readdir(dir))!= NULL)
 42      {
 43          // "。"を取り除く and ".." 
44          if(strcmp(filename-> d_name、" ")== 0 || 
 45              strcmp(filename-> d_name、" .. ")== 0 46              続行;
47          files.push_back(filename-> d_name);
48      }
 49 }

 

おすすめ

転載: www.cnblogs.com/buyizhiyou/p/12744611.html