获取磁盘剩余容量
#include <filesystem>
struct DiskSpaceInfo
{
double total;
double free;
double available;
};
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);
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;
return 0;
}