Skip to content

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