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 strPercent[100]{ 0 };

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

return strPercent;

}

相关推荐
saltymilk2 小时前
C++ 模板参数推导问题小记(模板类的模板构造函数)
c++·模板元编程
感哥2 小时前
C++ lambda 匿名函数
c++
沐怡旸8 小时前
【底层机制】std::unique_ptr 解决的痛点?是什么?如何实现?怎么正确使用?
c++·面试
感哥8 小时前
C++ 内存管理
c++
博笙困了15 小时前
AcWing学习——双指针算法
c++·算法
感哥15 小时前
C++ 指针和引用
c++
感哥1 天前
C++ 多态
c++
沐怡旸1 天前
【底层机制】std::string 解决的痛点?是什么?怎么实现的?怎么正确用?
c++·面试
River4161 天前
Javer 学 c++(十三):引用篇
c++·后端
感哥2 天前
C++ std::set
c++