boost :: filesystem :: space()报告错误的磁盘空间

时间:2019-07-08 10:02:53

标签: c++ windows boost diskspace boost-filesystem

驱动器C:上有430 GB的可用空间。但是对于这个程序:

#include <iostream>
#include <boost/filesystem.hpp>

int main()
{
    boost::filesystem::path p("C:");

    std::size_t freeSpace = boost::filesystem::space(p).free;
    std::cout<<freeSpace << " Bytes" <<std::endl;
    std::cout<<freeSpace / (1 << 20) << " MB"<<std::endl;

    std::size_t availableSpace = boost::filesystem::space(p).available;
    std::cout << availableSpace << " Bytes" <<std::endl;
    std::cout << availableSpace / (1 << 20) << " MB"<<std::endl;

    std::size_t totalSpace = boost::filesystem::space(p).capacity;
    std::cout << totalSpace << " Bytes" <<std::endl;
    std::cout << totalSpace / (1 << 20) << " MB"<<std::endl;

    return 0;
}

输出为:

2542768128 Bytes
2424 MB
2542768128 Bytes
2424 MB
2830102528 Bytes
2698 MB

我需要知道有多少可用磁盘空间,因为我的应用程序必须下载一个大文件,并且我需要知道下载它是否可行。

我在Windows上使用mingw:

g++ (i686-posix-dwarf-rev2, Built by MinGW-W64 project) 7.1.0

我还尝试使用MXE从Linux进行交叉编译:

i686-w64-mingw32.static-g++ (GCC) 5.5.0

两个都返回相同的数字。

2 个答案:

答案 0 :(得分:2)

使用boost::filesystem::space(p).free要求的类型。它可能需要64位整数类型:

uintmax_t freeSpace = boost::filesystem::space(p).free;

使用auto也很好。

答案 1 :(得分:2)

if (e instanceof MouseEvent) { // switch specific MouseEvent type here or inside another delegator handleMouseEvent(e); } else if (e instanceof TouchEvent) { handleTouchEvent(e); } 不能保证是最大的标准无符号类型。其实很少。

还有std::size_t defines space_info thus

boost::filesystem

使用struct space_info // returned by space function { uintmax_t capacity; uintmax_t free; uintmax_t available; // free space available to a non-privileged process }; 可以轻松避免错误,这很自然,因为确切的类型并不重要。几乎总是只有不匹配会造成伤害,因此Almost Always auto