如何在目录C ++的磁盘上获取大小

问题描述 投票:0回答:1

如何使用C ++代码获得磁盘上的大小而不是文件大小?

我正在尝试获取目录(文件夹)在磁盘上的大小,并且将文件大小加起来只能得出实际的文件大小。我需要的是磁盘上的大小,总之有吗?

谢谢

#include <iostream> 
using namespace std;
#include <filesystem>
using namespace std::filesystem;
int main(size_t argc, char* argv[])
{
   if (argc == 1) {
        void rscan3(path const&, unsigned i = 0);
        rscan3(".");

    }
}
void rscan3(path const& f, unsigned i) {
    string indent(i, ' ');
    uintmax_t size = 0;
    for (auto d : directory_iterator(f)) {

        size += d.file_size();
        cout << indent << "Folder = " << absolute(f).string() << " SIZE: " << size << endl;
        if (is_directory(d.status())) 
            rscan3(d.path(), i + 2);

    }
}
c++ filesystems filesize disk utility
1个回答
0
投票

文件由文件索引或文件表中的条目指示(对于NTFS为MFT,对于FAT文件系统为FAT)。该表位于物理文件系统(例如硬盘)的特殊位置,并保存每个文件的地址和元数据。文件大小是文件实际占用的空间量。可以为零或更大的尺寸。当文件中没有数据时,该文件为空(大小= 0)。

在Windows文件系统中,目录的大小是其中所有文件(和子文件夹中的文件)的总和。因此,一个空目录的大小为零。

Source: Why does an empty folder have zero bytes on Windows?

STL filesystem::file_size仅适用于文件,它为目录filesystem error: cannot get file size: Is a directory [dir]引发异常。为什么?因为目录只是文件索引或表中的一个条目。

© www.soinside.com 2019 - 2024. All rights reserved.