Reserve disk space before writing a file for efficiency
If you are using C++ 17, you should do it with std::filesystem::resize_file
Link
Changes the size of the regular file named by p as if by POSIX truncate: if the file size was previously larger than new_size, the remainder of the file is discarded. If the file was previously smaller than new_size, the file size is increased and the new area appears as if zero-filled.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <filesystem>
namespace fs = std::filesystem;
int main()
{
fs::path p = fs::current_path() / "example.bin";
std::ofstream(p).put('a');
fs::resize_file(p, 1024*1024*1024); // resize to 1 G
}
void ReserveSpace(LONG spaceLow, LONG spaceHigh, HANDLE hFile)
{
DWORD err = ::SetFilePointer(hFile, spaceLow, &spaceHigh, FILE_BEGIN);
if (err == INVALID_SET_FILE_POINTER) {
err = GetLastError();
// handle error
}
if (!::SetEndOfFile(hFile)) {
err = GetLastError();
// handle error
}
err = ::SetFilePointer(hFile, 0, 0, FILE_BEGIN); // reset
}
wRAR is correct. Open a new file using your favourite library, then seek to the penultimate byte and write a 0 there. That should allocate all the required disk space.