磁盘空间?(已使用/免费/总计)我如何获得这个?在 C++ 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1449055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disk Space? (used/free/total) how do I get this? in C++
提问by HoNgOuRu
Disk Space? (used/free/total) how do I get this? in C++... thanks just for reading.
磁盘空间?(已使用/免费/总计)我如何获得这个?在 C++ 中...感谢阅读。
回答by aJ.
GetDiskFreeSpaceExwin32 API
GetDiskFreeSpaceExwin32 API
回答by Nadir SOUALEM
#include <sys/statvfs.h>
#include <iostream>
#include <cstring>
using namespace std;
int main( int argc, char *argv[] )
{
struct statvfs fiData;
if( argc < 2 ) {
cout <<"Usage, ./size dir1 dir2 ... dirN\n";
return(1);
}
//Lets loopyloop through the argvs
for( int i= 1 ; i<argc; i++ ) {
if((statvfs(argv[i],&fiData)) < 0 ) {
cout << "\nFailed to stat:" << argv[i];
} else {
cout << "\nDisk: " << argv[i];
cout << "\nBlock size: "<< fiData.f_bsize;
cout << "\nTotal no blocks: "<< fiData.f_blocks;
cout << "\nFree blocks: "<< fiData.f_bfree;
}
}
}
Compilation:g++ -o size file.cpp
编译:g++ -o size file.cpp
Test:./size dir1 dir2
测试:./size dir1 dir2
回答by Nitinkumar Ambekar
The original Linux statfs() and fstatfs() system calls were not designed with extremely large file sizes in mind. Subsequently, Linux 2.6 added new statfs64() and fstatfs64() system calls that employ a new structure, statfs64. The new structure contains the same fields as the original statfs structure, but the sizes of various fields are increased, to accommodate large file sizes. see http://linux.die.net/man/2/statfs64
最初的 Linux statfs() 和 fstatfs() 系统调用在设计时并没有考虑到非常大的文件大小。随后,Linux 2.6 添加了新的 statfs64() 和 fstatfs64() 系统调用,它们采用了新的结构 statfs64。新结构包含与原始 statfs 结构相同的字段,但增加了各个字段的大小,以适应大文件。见http://linux.die.net/man/2/statfs64