当前位置:   article > 正文

windows和Linux c++遍历文件目录_linux c++遍历文件夹

linux c++遍历文件夹

1. windows文件目录遍历

  1. #include <string>
  2. #include <Windows.h>
  3. #include <io.h>
  4. #include <vector>
  5. using namespace std;
  6. void getFiles(const std::string& dir, std::vector<string>& files, const std::string filter_type="*.*")
  7. {
  8. //文件句柄
  9. intptr_t hFile = 0;
  10. //文件信息
  11. struct _finddata_t fileinfo;
  12. std::string path = dir + "\\" + filter_type;
  13. //string p = "F:\\TY_code\\sqliteR8\\TySrvDevelop\\bin\\TY-CM-2116A2-V1\\snap\\*.jpg";
  14. if ((hFile = _findfirst(path.c_str(), &fileinfo)) != -1)
  15. {
  16. do
  17. {
  18. //如果是目录,迭代之
  19. //如果不是,加入列表
  20. if ((fileinfo.attrib & _A_SUBDIR))
  21. {
  22. if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
  23. getFiles(fileinfo.name, files);
  24. }
  25. else
  26. {
  27. string dir = path.substr(0, path.find_last_of("\\")+1);
  28. files.push_back(dir + fileinfo.name);
  29. }
  30. } while (_findnext(hFile, &fileinfo) == 0);
  31. _findclose(hFile);
  32. }
  33. }

 2. Linux文件目录遍历

  1. #include <dirent.h>
  2. #include <string.h>
  3. #include <iostream>
  4. #include <vector>
  5. #include <string>
  6. namespace {
  7. void Usage(const char* exe)
  8. {
  9. fprintf(stderr, "input params error, run this exe as following command line:\n");
  10. fprintf(stderr, "\t%s arg1 arg2 arg3\n", exe);
  11. fprintf(stderr, "\targ1: specify the directory to traverse\n");
  12. fprintf(stderr, "\targ2: type:\n"
  13. "\t\t0: tarverse all files and all directories in directory;\n"
  14. "\t\t1: only tarverse all files, don't include directories in directory;\n"
  15. "\t\t2: only tarverse all directories, don't include files in directory.\n");
  16. fprintf(stderr, "\targ3: optional, filter, default is *, which is don't filter. \n");
  17. fprintf(stderr, "for example(support relative path), only traverse jpg image:\n");
  18. fprintf(stderr, "\t%s ./images 0 .jpg\n", exe);
  19. fprintf(stderr, "##### test fail #####\n");
  20. }
  21. // 遍历指定文件夹下的所有文件,不包括指定文件夹内的文件夹
  22. std::vector<std::string> GetListFiles(const std::string& path, const std::string& exten = "*")
  23. {
  24. std::vector<std::string> list;
  25. list.clear();
  26. DIR* dp = nullptr;
  27. struct dirent* dirp = nullptr;
  28. if ((dp = opendir(path.c_str())) == nullptr) {
  29. return list;
  30. }
  31. while ((dirp = readdir(dp)) != nullptr) {
  32. if (dirp->d_type == DT_REG) {
  33. if (exten.compare("*") == 0)
  34. list.emplace_back(static_cast<std::string>(dirp->d_name));
  35. else
  36. if (std::string(dirp->d_name).find(exten) != std::string::npos)
  37. list.emplace_back(static_cast<std::string>(dirp->d_name));
  38. }
  39. }
  40. closedir(dp);
  41. return list;
  42. }
  43. // 遍历指定文件夹下的所有文件夹,不包括指定文件夹下的文件
  44. std::vector<std::string> GetListFolders(const std::string& path, const std::string& exten = "*")
  45. {
  46. std::vector<std::string> list;
  47. list.clear();
  48. DIR* dp = nullptr;
  49. struct dirent* dirp = nullptr;
  50. if ((dp = opendir(path.c_str())) == nullptr) {
  51. return list;
  52. }
  53. while ((dirp = readdir(dp)) != nullptr) {
  54. if (dirp->d_type == DT_DIR && strcmp(dirp->d_name, ".") != 0 && strcmp(dirp->d_name, "..") != 0) {
  55. if (exten.compare("*") == 0)
  56. list.emplace_back(static_cast<std::string>(dirp->d_name));
  57. else
  58. if (std::string(dirp->d_name).find(exten) != std::string::npos)
  59. list.emplace_back(static_cast<std::string>(dirp->d_name));
  60. }
  61. }
  62. closedir(dp);
  63. return list;
  64. }
  65. // 遍历指定文件夹下的所有文件,包括指定文件夹内的文件夹
  66. std::vector<std::string> GetListFilesR(const std::string& path, const std::string& exten = "*")
  67. {
  68. std::vector<std::string> list = GetListFiles(path, exten);
  69. std::vector<std::string> dirs = GetListFolders(path, exten);
  70. for (auto it = dirs.cbegin(); it != dirs.cend(); ++it) {
  71. std::vector<std::string> cl = GetListFiles(*it, exten);
  72. for (auto file : cl) {
  73. list.emplace_back(*it + "/" + file);
  74. }
  75. }
  76. return list;
  77. }
  78. } // namespace
  79. int main(int argc, char* argv[])
  80. {
  81. if (argc < 3 || argc > 4) {
  82. Usage(argv[0]);
  83. return -1;
  84. }
  85. int type = atoi(argv[2]);
  86. std::string exten = "*";
  87. if (argc == 4) exten = std::string(argv[3]);
  88. std::vector<std::string> vec;
  89. if (type == 0) vec = GetListFilesR(std::string(argv[1]), exten);
  90. else if (type == 1) vec = GetListFiles(std::string(argv[1]), exten);
  91. else if (type == 2) vec = GetListFolders(std::string(argv[1]), exten);
  92. else { Usage(argv[0]); return -1;}
  93. fprintf(stdout, "traverse result: files count: %d\n", vec.size());
  94. for (auto& file : vec) {
  95. fprintf(stderr, "\t%s\n", file.c_str());
  96. }
  97. fprintf(stdout, "===== test success =====\n");
  98. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/716126
推荐阅读
相关标签
  

闽ICP备14008679号