目录大小

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

这篇关于目录大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Prevent class inheritance in C++(防止 C++ 中的类继承)
Why should I declare a virtual destructor for an abstract class in C++?(为什么要在 C++ 中为抽象类声明虚拟析构函数?)
Why is Default constructor called in virtual inheritance?(为什么在虚拟继承中调用默认构造函数?)
C++ cast to derived class(C++ 转换为派生类)
C++ virtual function return type(C++虚函数返回类型)
Is there any real risk to deriving from the C++ STL containers?(从 C++ STL 容器派生是否有任何真正的风险?)