#include <windows.h>
#include <iostream>
#include <string>
#include <iomanip>
int main() {
std::wstring rootPath = L"C:\\";
ULARGE_INTEGER freeBytesAvailable{};
ULARGE_INTEGER totalBytes{};
ULARGE_INTEGER totalFreeBytes{};
BOOL ok = GetDiskFreeSpaceExW(
rootPath.c_str(),
&freeBytesAvailable,
&totalBytes,
&totalFreeBytes
);
if (!ok) {
std::wcerr << L"Error: " << GetLastError() << L"\n";
return 1;
}
constexpr double GB = 1024.0 * 1024.0 * 1024.0;
double totalGB = static_cast<double>(totalBytes.QuadPart) / GB;
double freeGB = static_cast<double>(totalFreeBytes.QuadPart) / GB;
std::wcout << L"Disk: " << rootPath << L"\n";
std::wcout << L"Total size: " << totalBytes.QuadPart
<< L" bytes (" << std::fixed << std::setprecision(2)
<< totalGB << L" GB)\n";
std::wcout << L"Free space: " << totalFreeBytes.QuadPart
<< L" bytes (" << std::fixed << std::setprecision(2)
<< freeGB << L" GB)\n";
return 0;
}
/*
run:
Disk: C:\
Total size: 999559262208 bytes (930.91 GB)
Free space: 284177965056 bytes (264.66 GB)
*/