I have noticed a huge performance hit in one of my projects when logging is enabled for the first time. But when the log file limit is reached and the program starts writing
If you're using C#, you can call SetLength
on a FileStream
to instantly set an initial size to the file.
e.g.
using (var fileStream = File.Open(@"file.txt", FileMode.Create, FileAccess.Write, FileShare.Read))
{
fileStream.SetLength(1024 * 1024 * 1024); // Reserve 1 GB
}
Here's a simple function that will work for files of any size:
void SetFileSize(HANDLE hFile, LARGE_INTEGER size)
{
SetFilePointer(hFile, size, NULL, FILE_BEGIN);
SetEndOfFile(hFile);
}
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
}
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
}
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.
You can use the SetFileValidData function to extend the logical length of a file without having to write out all that data to disk. However, because it can allow to read disk data to which you may not otherwise have been privileged, it requires the SE_MANAGE_VOLUME_NAME
privilege to use. Carefully read the Remarks section of the documentation.
Also implementation of SetFileValidData depend on fs driver. NTFS support it and FAT only since Win7.