Prepending Data to a File

后端 未结 2 437
孤街浪徒
孤街浪徒 2020-12-11 18:21

There\'s no way in any operating system I\'m aware of for a program to prepend data to a file efficiently. And yet, this doesn\'t seem difficult -- the file system

2条回答
  •  -上瘾入骨i
    2020-12-11 18:42

    In prepending or appending data to a file, there is always an issue of allocating space. Appending additional space to a file is much easier than prepending because of file descriptors pointing to the beginning of a file's stream. If you want to append to a file, the file descriptor need not be changed, just the size of the file and the allocated memory. If you want to prepend to a file, a new file descriptor must be immediately instantiated, either to write the prepended data to first or to store the location of the data being prepended while the original is updated.

    Making a new file descriptor can be tricky, as you must also update any references pointing to it. This is why it is easy for an OS to implement appending data and slightly harder to implement prepending.

提交回复
热议问题