如何使用C++标准库获取目录中的文件总数?
cotxawn71#
如果你不排除基本上总是可用的C标准库,你可以使用它,因为它在任何地方都可用,不像boost,它是一个非常有用的选择!例如here.还有这里:
#include <stdio.h>#include <sys/types.h>#include <dirent.h>int main (void){ DIR *dp; int i = 0; struct dirent *ep; dp = opendir ("./"); if (dp != NULL) { while (ep = readdir (dp)) i++; (void) closedir (dp); } else perror ("Couldn't open the directory"); printf("There's %d files in the current directory.\n", i); return 0;}
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
int main (void)
{
DIR *dp;
int i = 0;
struct dirent *ep;
dp = opendir ("./");
if (dp != NULL)
while (ep = readdir (dp))
i++;
(void) closedir (dp);
}
else
perror ("Couldn't open the directory");
printf("There's %d files in the current directory.\n", i);
return 0;
字符串果然
> $ ls -a | wc -l138 > $ ./countThere's 138 files in the current directory.
> $ ls -a | wc -l
138
> $ ./count
There's 138 files in the current directory.
型这根本不是C++,但它可以在大多数(如果不是全部的话)操作系统上使用,并且无论如何都可以在C++中工作。
**更新:**我将更正我之前关于它是C标准库的一部分的说法-它不是。但是你可以把这个概念带到其他操作系统,因为它们都有自己的方法来处理文件,而不必抓取额外的库。编辑::添加了i的初始化
tvmytwxo2#
不能。最接近的方法是使用类似Boost.Filesystem的东西编辑:C++17可以使用STL的filesystem库
b4lqfgs43#
从C++17开始,它可以用STL来完成:
auto dirIter = std::filesystem::directory_iterator("directory_path");int fileCount = std::count_if( begin(dirIter), end(dirIter), [](auto& entry) { return entry.is_regular_file(); });
auto dirIter = std::filesystem::directory_iterator("directory_path");
int fileCount = std::count_if(
begin(dirIter),
end(dirIter),
[](auto& entry) { return entry.is_regular_file(); }
);
字符串一个简单的for循环也可以工作:
auto dirIter = std::filesystem::directory_iterator("directory_path");int fileCount = 0;for (auto& entry : dirIter){ if (entry.is_regular_file()) { ++fileCount; }}
int fileCount = 0;
for (auto& entry : dirIter)
if (entry.is_regular_file())
++fileCount;
型参见https://en.cppreference.com/w/cpp/filesystem/directory_iterator
ekqde3dh4#
一个老问题,但因为它首先出现在谷歌搜索,我想添加我的答案,因为我需要这样的东西。
int findNumberOfFilesInDirectory(std::string& path){ int counter = 0; WIN32_FIND_DATA ffd; HANDLE hFind = INVALID_HANDLE_VALUE; // Start iterating over the files in the path directory. hFind = ::FindFirstFileA (path.c_str(), &ffd); if (hFind != INVALID_HANDLE_VALUE) { do // Managed to locate and create an handle to that folder. { counter++; } while (::FindNextFile(hFind, &ffd) == TRUE); ::FindClose(hFind); } else { printf("Failed to find path: %s", path.c_str()); } return counter;}
int findNumberOfFilesInDirectory(std::string& path)
int counter = 0;
WIN32_FIND_DATA ffd;
HANDLE hFind = INVALID_HANDLE_VALUE;
// Start iterating over the files in the path directory.
hFind = ::FindFirstFileA (path.c_str(), &ffd);
if (hFind != INVALID_HANDLE_VALUE)
do // Managed to locate and create an handle to that folder.
counter++;
} while (::FindNextFile(hFind, &ffd) == TRUE);
::FindClose(hFind);
} else {
printf("Failed to find path: %s", path.c_str());
return counter;
字符串
m0rkklqb5#
如果它们命名良好,排序良好,并且具有相同的扩展名,则可以简单地使用标准C++库来计算它们。假设文件名为“img_0.jpg..img_10000.jpg..img_n.jpg”,只需检查它们是否在文件夹中。
int Trainer::fileCounter(string dir, string prefix, string extension){ int returnedCount = 0; int possibleMax = 5000000; //some number you can expect. for (int istarter = 0; istarter < possibleMax; istarter++){ string fileName = ""; fileName.append(dir); fileName.append(prefix); fileName.append(to_string(istarter)); fileName.append(extension); bool status = FileExistenceCheck(fileName); returnedCount = istarter; if (!status) break; } return returnedCount;}bool Trainer::FileExistenceCheck(const std::string& name) { struct stat buffer; return (stat(name.c_str(), &buffer) == 0);}
int Trainer::fileCounter(string dir, string prefix, string extension)
int returnedCount = 0;
int possibleMax = 5000000; //some number you can expect.
for (int istarter = 0; istarter < possibleMax; istarter++){
string fileName = "";
fileName.append(dir);
fileName.append(prefix);
fileName.append(to_string(istarter));
fileName.append(extension);
bool status = FileExistenceCheck(fileName);
returnedCount = istarter;
if (!status)
break;
return returnedCount;
bool Trainer::FileExistenceCheck(const std::string& name) {
struct stat buffer;
return (stat(name.c_str(), &buffer) == 0);
xlpyo6sf6#
您需要使用本机API或框架。
vfhzx4xs7#
auto it = std::filesystem::directory_iterator{"myDir"}; // or `recursive_directory_iterator`
字符串计算一切:
std::distance(it, {});
型仅计数常规文件:
std::count_if(it, {}, [](auto& x){return x.is_regular_file(); });
型(do在迭代器被修改后不要重用它,或者要小心。
7条答案
按热度按时间cotxawn71#
如果你不排除基本上总是可用的C标准库,你可以使用它,因为它在任何地方都可用,不像boost,它是一个非常有用的选择!
例如here.
还有这里:
字符串
果然
型
这根本不是C++,但它可以在大多数(如果不是全部的话)操作系统上使用,并且无论如何都可以在C++中工作。
**更新:**我将更正我之前关于它是C标准库的一部分的说法-它不是。但是你可以把这个概念带到其他操作系统,因为它们都有自己的方法来处理文件,而不必抓取额外的库。
编辑::添加了i的初始化
tvmytwxo2#
不能。最接近的方法是使用类似Boost.Filesystem的东西
编辑:C++17可以使用STL的filesystem库
b4lqfgs43#
从C++17开始,它可以用STL来完成:
字符串
一个简单的for循环也可以工作:
型
参见https://en.cppreference.com/w/cpp/filesystem/directory_iterator
ekqde3dh4#
一个老问题,但因为它首先出现在谷歌搜索,我想添加我的答案,因为我需要这样的东西。
字符串
m0rkklqb5#
如果它们命名良好,排序良好,并且具有相同的扩展名,则可以简单地使用标准C++库来计算它们。
假设文件名为“img_0.jpg..img_10000.jpg..img_n.jpg”,只需检查它们是否在文件夹中。
字符串
xlpyo6sf6#
您需要使用本机API或框架。
vfhzx4xs7#
字符串
计算一切:
型
仅计数常规文件:
型
(do在迭代器被修改后不要重用它,或者要小心。