Size of a directory(目录大小)
问题描述
有没有办法在不实际遍历此目录并添加其中每个文件的大小的情况下获取目录大小/文件夹大小?理想情况下想使用一些像 boost 这样的库,但 win api 也可以.
Is there a way to get the directory size/folder size without actually traversing this directory and adding size of each file in it? Ideally would like to use some library like boost but win api would be ok too.
推荐答案
据我所知,您必须在大多数操作系统上通过迭代来做到这一点.
As far as I am aware you have to do this with iteration on most operating systems.
你可以看一下 boost.filesystem,这个库有一个 recursive_directory_iterator,它会迭代,尽管系统上的任何文件都得到了累积的大小.
You could take a look at boost.filesystem, this library has a recursive_directory_iterator, it will iterate though ever file on the system getting accumulation the size.
http://www.boost.org/doc/libs/1_49_0/libs/filesystem/v3/doc/reference.html#Class-recursive_directory_iterator
include <boost/filesystem.hpp>
int main()
{
namespace bf=boost::filesystem;
size_t size=0;
for(bf::recursive_directory_iterator it("path");
it!=bf::recursive_directory_iterator();
++it)
{
if(!bf::is_directory(*it))
size+=bf::file_size(*it);
}
}
PS:你可以通过使用 std::accumulate 和一个 lambda 我只是 CBA 使这更干净
PS: you can make this a lot cleaner by using std::accumulate and a lambda I just CBA
这篇关于目录大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:目录大小
基础教程推荐
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- 提升 ASIO 流缓冲 2021-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
- c++ STL设置差异 2022-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
