如何使用 boost::文件系统计算目录中的文件数
How do I count the number of files in a directory using boost::filesystem?
我得到了一个提升::文件系统::p ath。有没有一种快速的方法可以获取路径指向的目录中的文件数?
这是标准C++中的一行
:#include <iostream>
#include <boost/filesystem.hpp>
#include <boost/lambda/bind.hpp>
int main()
{
using namespace boost::filesystem;
using namespace boost::lambda;
path the_path( "/home/myhome" );
int cnt = std::count_if(
directory_iterator(the_path),
directory_iterator(),
static_cast<bool(*)(const path&)>(is_regular_file) );
// a little explanation is required here,
// we need to use static_cast to specify which version of
// `is_regular_file` function we intend to use
// and implicit conversion from `directory_entry` to the
// `filesystem::path` will occur
std::cout << cnt << std::endl;
return 0;
}
您可以使用以下命令循环访问目录中的文件:
for(directory_iterator it(YourPath); it != directory_iterator(); ++it)
{
// increment variable here
}
或递归:
for(recursive_directory_iterator it(YourPath); it != recursive_directory_iterator(); ++it)
{
// increment variable here
}
你可以在这里找到一些简单的例子。
directory_iterator begin(the_path), end;
int n = count_if(begin, end,
[](const directory_entry & d) {
return !is_directory(d.path());
});
相关文章:
- 如何计算文件中的"columns"数?
- C++数据文件、数组和计算赋值
- 如何计算文件中的生物数量?
- 计算二进制文件中的位数
- 通过从文件中读取值来计算移动平均线
- 在目标计算机上访问 POSIX 信号灯时出现可执行文件崩溃(SEGV_MAPERR)
- 我正在尝试计算文本文件中有多少销售人员 c++
- 在C++中对文件中的值单独执行计算
- 从文本文件中的每一行读取数字,并计算数字重复的次数
- RPN计算器使用头文件进行计算操作和堆栈;用于堆栈的矢量
- 从文件中找出字符'a',然后计算字符出现的行数
- 使用结构数组计算文本文件中单词的出现次数C++
- 快速计算 CSV 文件 C++ 中的行数
- 创建程序以从给定的.txt文件中查找文本,替换并计算单词
- 通过将文本文件读取为字符串/向量来计算加权/未加权 GPA
- 在 64 位 Linux armv8 计算机上编译 32 位二进制文件时遇到问题
- 阅读一个大文件来计算重复K次的单词数
- 在不同的计算机上运行.exe文件.(视觉工作室)
- 使用Proc文件计算CPU使用率%
- 从文本文件计算