使用C++计算目录中的文件数

aemubtdh  于 2024-01-09  发布在  其他
关注(0)|答案(7)|浏览(184)

如何使用C++标准库获取目录中的文件总数?

cotxawn7

cotxawn71#

如果你不排除基本上总是可用的C标准库,你可以使用它,因为它在任何地方都可用,不像boost,它是一个非常有用的选择!
例如here.
还有这里:

  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <dirent.h>
  4. int main (void)
  5. {
  6. DIR *dp;
  7. int i = 0;
  8. struct dirent *ep;
  9. dp = opendir ("./");
  10. if (dp != NULL)
  11. {
  12. while (ep = readdir (dp))
  13. i++;
  14. (void) closedir (dp);
  15. }
  16. else
  17. perror ("Couldn't open the directory");
  18. printf("There's %d files in the current directory.\n", i);
  19. return 0;
  20. }

字符串
果然

  1. > $ ls -a | wc -l
  2. 138
  3. > $ ./count
  4. There's 138 files in the current directory.


这根本不是C++,但它可以在大多数(如果不是全部的话)操作系统上使用,并且无论如何都可以在C++中工作。

**更新:**我将更正我之前关于它是C标准库的一部分的说法-它不是。但是你可以把这个概念带到其他操作系统,因为它们都有自己的方法来处理文件,而不必抓取额外的库。
编辑::添加了i的初始化

展开查看全部
tvmytwxo

tvmytwxo2#

不能。最接近的方法是使用类似Boost.Filesystem的东西
编辑:C++17可以使用STL的filesystem

b4lqfgs4

b4lqfgs43#

从C++17开始,它可以用STL来完成:

  1. auto dirIter = std::filesystem::directory_iterator("directory_path");
  2. int fileCount = std::count_if(
  3. begin(dirIter),
  4. end(dirIter),
  5. [](auto& entry) { return entry.is_regular_file(); }
  6. );

字符串
一个简单的for循环也可以工作:

  1. auto dirIter = std::filesystem::directory_iterator("directory_path");
  2. int fileCount = 0;
  3. for (auto& entry : dirIter)
  4. {
  5. if (entry.is_regular_file())
  6. {
  7. ++fileCount;
  8. }
  9. }


参见https://en.cppreference.com/w/cpp/filesystem/directory_iterator

展开查看全部
ekqde3dh

ekqde3dh4#

一个老问题,但因为它首先出现在谷歌搜索,我想添加我的答案,因为我需要这样的东西。

  1. int findNumberOfFilesInDirectory(std::string& path)
  2. {
  3. int counter = 0;
  4. WIN32_FIND_DATA ffd;
  5. HANDLE hFind = INVALID_HANDLE_VALUE;
  6. // Start iterating over the files in the path directory.
  7. hFind = ::FindFirstFileA (path.c_str(), &ffd);
  8. if (hFind != INVALID_HANDLE_VALUE)
  9. {
  10. do // Managed to locate and create an handle to that folder.
  11. {
  12. counter++;
  13. } while (::FindNextFile(hFind, &ffd) == TRUE);
  14. ::FindClose(hFind);
  15. } else {
  16. printf("Failed to find path: %s", path.c_str());
  17. }
  18. return counter;
  19. }

字符串

展开查看全部
m0rkklqb

m0rkklqb5#

如果它们命名良好,排序良好,并且具有相同的扩展名,则可以简单地使用标准C++库来计算它们。
假设文件名为“img_0.jpg..img_10000.jpg..img_n.jpg”,只需检查它们是否在文件夹中。

  1. int Trainer::fileCounter(string dir, string prefix, string extension)
  2. {
  3. int returnedCount = 0;
  4. int possibleMax = 5000000; //some number you can expect.
  5. for (int istarter = 0; istarter < possibleMax; istarter++){
  6. string fileName = "";
  7. fileName.append(dir);
  8. fileName.append(prefix);
  9. fileName.append(to_string(istarter));
  10. fileName.append(extension);
  11. bool status = FileExistenceCheck(fileName);
  12. returnedCount = istarter;
  13. if (!status)
  14. break;
  15. }
  16. return returnedCount;
  17. }
  18. bool Trainer::FileExistenceCheck(const std::string& name) {
  19. struct stat buffer;
  20. return (stat(name.c_str(), &buffer) == 0);
  21. }

字符串

展开查看全部
xlpyo6sf

xlpyo6sf6#

您需要使用本机API或框架。

vfhzx4xs

vfhzx4xs7#

  1. auto it = std::filesystem::directory_iterator{"myDir"}; // or `recursive_directory_iterator`

字符串
计算一切:

  1. std::distance(it, {});


仅计数常规文件:

  1. std::count_if(it, {}, [](auto& x){return x.is_regular_file(); });


(do在迭代器被修改后不要重用它,或者要小心。

相关问题