linux下用C语言获取硬盘/SD/FLASH存储容量及使用情况

#include                                                                                                                   
#include 
#include 
#include 

int main()
{
    struct statfs diskInfo;  
    statfs("/", &diskInfo);  
    unsigned long long totalBlocks = diskInfo.f_bsize;  
    unsigned long long totalSize = totalBlocks * diskInfo.f_blocks;  
    size_t mbTotalsize = totalSize>>20;  
    unsigned long long freeDisk = diskInfo.f_bfree*totalBlocks;  
    size_t mbFreedisk = freeDisk>>20;  
    printf ("/  total=%dMB, free=%dMB\n", mbTotalsize, mbFreedisk);  

    statfs("/boot", &diskInfo);  
    totalBlocks = diskInfo.f_bsize;  
    totalSize = totalBlocks * diskInfo.f_blocks;  
    mbTotalsize = totalSize>>20;  
    freeDisk = diskInfo.f_bfree*totalBlocks;  
    mbFreedisk = freeDisk>>20;  
    printf ("/boot  total=%dMB, free=%dMB\n", mbTotalsize, mbFreedisk);
    statfs("/dev/shm", &diskInfo);
    totalBlocks = diskInfo.f_bsize;
    totalSize = totalBlocks * diskInfo.f_blocks;
    mbTotalsize = totalSize>>20;
    freeDisk = diskInfo.f_bfree*totalBlocks;
    mbFreedisk = freeDisk>>20;
    printf ("/dev/shm  total=%dMB, free=%dMB\n", mbTotalsize, mbFreedisk);
}

你可能感兴趣的:(c/c++)