使用 C++ 的目录中的文件计数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2802188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
File count in a directory using C++
提问by harik
How do I get the total number of files in a directory by using C++ standard library? Any help is appreciated.
如何使用 C++ 标准库获取目录中的文件总数?任何帮助表示赞赏。
采纳答案by Yacoby
You can't. The closest you are going to be able to get is to use something like Boost.Filesystem
你不能。您将能够获得的最接近的是使用Boost.Filesystem 之类的东西
回答by LukeN
If you don't exclude the basically always available C standard library, you can use that one. Because it's available everywhere anyways, unlike boost, it's a pretty usable option!
如果您不排除基本上始终可用的 C 标准库,则可以使用该库。因为它在任何地方都可用,与 boost 不同,它是一个非常有用的选择!
An example is given here.
And 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;
}
And sure enough
果然
> $ ls -a | wc -l
138
> $ ./count
There's 138 files in the current directory.
This isn't C++ at all, but it is available on most, if not all, operating systems, and will work in C++ regardless.
这根本不是 C++,但它可以在大多数(如果不是全部)操作系统上使用,并且无论如何都可以在 C++ 中运行。
UPDATE:I'll correct my previous statement about this being part of the C standard library - it's not. But you can carry this concept to other operating systems, because they all have their ways of dealing with files without having to grab out additional libraries.
更新:我将更正我之前关于这是 C 标准库的一部分的声明——它不是。但是您可以将此概念应用于其他操作系统,因为它们都有自己的处理文件的方式,而无需获取额外的库。
EDIT:: Added initialization of i
编辑::添加了 i 的初始化
回答by Tal T
An old question, but since it appears first on Google search, I thought to add my answer since I had a need for something like that.
一个老问题,但由于它首先出现在谷歌搜索上,我想添加我的答案,因为我需要类似的东西。
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;
}
回答by John Doggett
As of C++17 it can be done with STL:
从 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(); }
);
A simple for-loop works, too:
一个简单的 for 循环也有效:
auto dirIter = std::filesystem::directory_iterator("directory_path");
int fileCount = 0;
for (auto& entry : dirIter)
{
if (entry.is_regular_file())
{
++fileCount;
}
}
See https://en.cppreference.com/w/cpp/filesystem/directory_iterator
请参阅https://en.cppreference.com/w/cpp/filesystem/directory_iterator
回答by John Doggett
You would need to use a native API or framework.
您将需要使用本机 API 或框架。
回答by user1098761
If they are well named, sorted, and have the same extension, you could simply do count them with standard C++ library.
如果它们被命名、排序并具有相同的扩展名,您可以简单地使用标准 C++ 库来计算它们。
Assume the file names are like "img_0.jpg..img_10000.jpg..img_n.jpg", Just check if they are in the folder or not.
假设文件名类似于“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);
}