linux获取磁盘信息

/// <summary>

/// 获取linux磁盘信息

/// </summary>

/// <returns></returns>

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 strPercent100{ 0 };

sprintf(strPercent, "%.2f%", usagePercentage);

return strPercent;

}

相关推荐
clint4563 天前
C++进阶(1)——前景提要
c++
夜悊3 天前
C++代码示例:进制数简单生成工具
c++
郝学胜_神的一滴3 天前
CMake 021: IF 条件判据详诠
c++·cmake
_wyt0014 天前
洛谷 B3930 [GESP202312 五级] 烹饪问题 题解
c++·gesp
LDR0064 天前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言
雪碧聊技术4 天前
Tree.js是什么?一文讲透
开发语言·javascript·ecmascript
码云数智-园园4 天前
C++20 Modules 模块详解
java·开发语言·spring
swordbob4 天前
NIO的channel中什么是 fd(File Descriptor,文件描述符)
java·开发语言·nio
源分享4 天前
Java线程同步的多种实现方法(非常详细)
java·开发语言·jvm
Luminous.4 天前
C语言--day30
c语言·开发语言