I want to insert into a large file, but I do not want to copy the part that is behind the insertion point. Is there a way to do this (of course with restrictions from the file system)? I'm thinking along the lines that the file system is normally organized in blocks & inserting a full block between 2 blocks should be cheap. Does anyone know if and how this could be done on win32 systems?
-
yes, it's a duplicate. Thank you - I did not find it. – Tobias Langner Nov 04 '13 at 14:13
-
It was presented to you as part of a long list of possible duplicates as you wrote your question. Please take more care in the future. – Lightness Races in Orbit Nov 04 '13 at 14:25
-
yes, but it's title misses the important part - without copying. It's title also does not mention that the file is large. And in general, I know how it is possible to insert into a file using win32 api – Tobias Langner Nov 05 '13 at 10:41
-
It is possible to click on the title and read the actual question. It only takes a moment. – Lightness Races in Orbit Nov 05 '13 at 10:52
2 Answers
The simple answer is no. The OS doesn't support this, and for file systems like those used by Windows or Unix, it's difficult to see how they could. It's true that physically, the file itself is split into allocation blocks, but the size of these blocks varies from drive to drive, not to mention from file system to file system (and you'll often have several different file systems mounted), and the utility of a function which allows you to insert, but only multiples of n bytes, at a position which is itself a multple of n bytes, probably seems of too limited utility to be worthwhile. And supporting it would still entail copying, since the blocks containing the pointers to these blocks would have to be copied.

- 150,581
- 18
- 184
- 329
You can truncate, append new data, append remaining old data.

- 378,754
- 76
- 643
- 1,055
-
You'd still have to read the old data at the end first. And the results wouldn't be pleasant if your program was terminated right after the truncation. – James Kanze Nov 04 '13 at 14:07
-
@JamesKanze: No matter what you do, you can break it. Pull the power while the hard disk cache is being emptied and let me know what happens – Lightness Races in Orbit Nov 04 '13 at 14:10
-
The usual techniques always leave a consistent state somewhere. You don't remove or modify the source until the destination has been successfully written. – James Kanze Nov 04 '13 at 14:39
-
@JamesKanze: It depends what you're doing. If you're manipulating a 500GB file then, no, you probably don't leave a consistent state somewhere unless you count your daily backups _[disclaimer: I entirely jest]_ – Lightness Races in Orbit Nov 04 '13 at 14:40