获取磁盘剩余容量
#include <filesystem>
struct DiskSpaceInfo
{
double total{ 0.0 };
double free{ 0.0 };
double available{ 0.0 };
double used_rate{ 0.0 };
double available_rate{ 0.0 };
};
DiskSpaceInfo getDiskSpace(const std::string& path)
{
std::filesystem::space_info si = std::filesystem::space(path);
DiskSpaceInfo info;
info.total = static_cast<double>(si.capacity) / (1024 * 1024 * 1024);
info.free = static_cast<double>(si.free) / (1024 * 1024 * 1024);
info.available = static_cast<double>(si.available) / (1024 * 1024 * 1024);
info.used_rate = (info.total - info.available) / info.total;
info.available_rate = info.available / info.total;
return info;
}
int main()
{
DiskSpaceInfo info = getDiskSpace("D:\\");
std::cout << "Total space: " << info.total << " GB" << std::endl;
std::cout << "Free space: " << info.free << " GB" << std::endl;
std::cout << "Available space: " << info.available << " GB" << std::endl;
std::cout << "Used space rate: " << info.used_rate << std::endl;
std::cout << "Available space rate: " << info.available_rate << std::endl;
return 0;
}