There is no simple way to prepend to a file¶
When writing software, there isn't an easy way to write to the start of a file. It usually involves reading the whole file (whether at once or in chunks) and adding the part you want to the start of the file.
This is because of how filesystems tend to store files on disk. The description of a file is it's length and where it starts. You can't change where it starts because something else could already be there. To add to the start, then, you have to copy the whole lot of the file and 'shift' it down a little.
It's true that there can be more flexibility here, but this depends on specific filesystems.
References¶
Backlinks¶
- CAN's scratch command should push existing scratchfile content below the fold
- I'll do some research into a simple interface for prepending to a file on disk. I can always use the naieve way of reading the whole file and sticking the string we want onto the start for now if nothing useful pops up, and think about the problem more deeply if/when the naieve way causes problems. The long-and-short of it is that there is no simple way to prepend to a file on disk.