[英]How can get disk usage in linux using c program?
I want to get disk usage in linux but when I run above code, the result is wrong.我想在 linux 中获取磁盘使用情况,但是当我运行上面的代码时,结果是错误的。
#include <stdio.h>
#include <sys/statvfs.h>
int main(int argc, const char *argv[])
{
const unsigned int GB = (1024 * 1024) * 1024;
struct statvfs buffer;
int ret = statvfs("/dev/sda4", &buffer);
if (!ret) {
const double total = (double)(buffer.f_blocks * buffer.f_frsize) / GB;
const double available = (double)(buffer.f_bfree * buffer.f_frsize) / GB;
const double used = total - available;
const double usedPercentage = (double)(used / total) * (double)100;
printf("Total: %f --> %.0f\n", total, total);
printf("Available: %f --> %.0f\n", available, available);
printf("Used: %f --> %.1f\n", used, used);
printf("Used Percentage: %f --> %.0f\n", usedPercentage, usedPercentage);
}
return ret;
}
The output is:输出是:
Total: 7.757446 --> 8
Available: 7.757446 --> 8
Used: 0.000000 --> 0.0
Used Percentage: 0.000000 --> 0
I run df command in linux terminal.我在 linux 终端中运行 df 命令。 The output is:
输出是:
Filesystem 1K-blocks Used Available Use% Mounted on
**/dev/sda4 471705824 13885152 433789596 4% /**
These two result is different from each other.这两个结果互不相同。 What is the reason?
是什么原因?
You can not use the device "/dev/sda4".您不能使用设备“/dev/sda4”。 You have to use a path, where the device has been mounted like "/" or "/home".
您必须使用路径,其中设备已安装,如“/”或“/home”。 If you use the device name, you get the result for the "udev" filesystem.
如果使用设备名称,则会得到“udev”文件系统的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.