I'm working on some legacy code that uses win32 WriteFile()
to write to a random location in a binary file. The offset of the write can be past the end of the file in which case WriteFile()
seems to automatically extend the file size to the offset and then write the data to the file.
I'd like to use std::fstream
to do the same, but when I try to seekp()
to the appropriate location, past the end of the file, the seekp()
fails and the subsequent write()
fails as well.
So it seems to me that I have to 'manually' fill in the space between the current EOF and the location I want to write to.
The code looks like this:
void Save(size_t offset, const Element& element)
{
m_File.seekp(offset, std::ios_base::beg);
m_File.write(reinterpret_cast<const char*>(&element), sizeof(Element));
if (m_File.fail()) {
// ... error handling
}
}
So is my only option to 'manually' write 0
s from the current EOF up to offset
?