没啥好说的吧,直接看代码
#include<sys/statfs.h>
#include<stdio.h>
/**
*获取磁盘容量情况
**/
int main()
{
struct statfs diskInfo;
statfs("/media/ubuntu/yu",&diskInfo);
unsigned long long blocksize=diskInfo.f_bsize;
unsigned long long totalsize=blocksize*diskInfo.f_blocks;
printf("Total_size=%llu B =%llu KB =%llu MB =%llu GB\n",
totalsize,totalsize>>10,totalsize>>20,totalsize>>30);
unsigned long long freeDisk=diskInfo.f_bfree*blocksize;
unsigned long long availableDisk=diskInfo.f_bavail*blocksize;
printf("Disk_free=%llu MB =%llu GB\nDisk_available=%llu MB =%llu GB =%llu TB\n",
freeDisk>>20,freeDisk>>30,availableDisk>>20,availableDisk>>30,availableDisk>>40);
return 0;
}