gpt4 book ai didi

c++ - 获取目录的大小(而不是目录的大小)

转载 作者:行者123 更新时间:2023-12-03 07:02:06 25 4
gpt4 key购买 nike

(请先解决此问题,再阅读完整的问题)
在C++中,我们可以通过运行std::filesystem::file_size(PATH);来获取常规文件的文件大小,但是此功能不适用于目录,这是我的问题。
我处于一种情况,我需要知道目录“inode”的大小,在(大多数)Linux系统中,目录的标准大小为4kB或块大小:

$:~/tmp/test$ mkdir ex
$:~/tmp/test$ ls -l
total 4
drwxrwxr-x 2 secret secret 4096 Oct 15 08:43 ex
这些4kB包含用于在该目录中包含文件“列表”的空间。
但是,如果目录中的文件数量变得非常大,则文件夹的大小可能会增加(这就是我所在的位置)。
我需要能够跟踪这种增长。
所以我的问题是,除了从C++调用 ls -ldu外,还有一种C++本地方法来获取目录的大小吗?
我知道它不适用于 std::filesystem::file_size(path)的原因是由于文件系统以不同的方式表示目录。

最佳答案

https://en.cppreference.com/w/cpp/filesystem/file_size

For a regular file p, returns the size determined as if by reading thest_size member of the structure obtained by POSIX stat (symlinks arefollowed)

The result of attempting to determine the size of a directory (as wellas any other file that is not a regular file or a symlink) isimplementation-defined.


目录中的 file_size实际上可能在某些实现中有效,但是我想您的无效。似乎没有纯粹的C++替代品,但是您可以自己调用POSIX stat函数。那确实适用于目录并报告您想要的号码。

关于c++ - 获取目录的大小(而不是目录的大小),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64366216/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com