I am looking for a way to make speedy modifications to large multi-gigabyte files. Do the Win32 API support the ability to insert text into a file at a specific offset without having to rewrite the entire file back to disk from the very beginning or from the offset of the change?
Consider an example. Let’s say we have the text “test” repeated over and over in a file that is 1 GB in size. If I want to go to the 500 MB offset and insert the text “new”, is there a way to insert it without having to rewrite the entire file from the beginning, and/or without having to rewrite the last 500 MBs of it?
Can it be done using Win32 API? If not, are there any strategies to optimize a text insertion operation like this to maximize speed?
There are methods to rewrite only the portion after the insertion point, but generally, no – to insert something at a particular point in a file, you must re-write everything after that point.
This boils down to the way files are stored on disk – typically in chunks, such that this operation is either not possible or not easy. For 99% of the cases, this doesn’t matter, so the API doesn’t expose a way of doing this.
If you have control over the file format, you can engineer ways such that you can write data to the end of the file, but have some tracking data to say “this stuff really belongs here”.