linux获取磁盘信息

///


/// 获取linux磁盘信息
///

///
std::string ZmApiDeviceInfo::GetLinuxStorage()
{
    struct statvfs vfs;

    const char* path = "/"; // 要检查的文件系统路径
    if (statvfs(path, &vfs) != 0) {
        std::cerr << "Error getting file system status." << std::endl;
        return "";
    }

    unsigned long long totalSpace = vfs.f_frsize * vfs.f_blocks; // 总磁盘空间
    unsigned long long freeSpace = vfs.f_frsize * vfs.f_bfree;   // 空闲磁盘空间
    unsigned long long usedSpace = totalSpace - freeSpace;       // 已用磁盘空间

    double usagePercentage = (double)usedSpace / totalSpace * 100.0;

    /*std::cout << "Total Space: " << totalSpace << " bytes" << std::endl;
    std::cout << "Used Space: " << usedSpace << " bytes" << std::endl;
    std::cout << "Free Space: " << freeSpace << " bytes" << std::endl;
    std::cout << "Usage Percentage: " << usagePercentage << "%" << std::endl;*/

    char strPercent[100]{ 0 };
    sprintf(strPercent, "%.2f%", usagePercentage);

    return strPercent;
}

你可能感兴趣的:(c++,开发语言)