How to prepend to a file (add at the top)

后端 未结 8 998
逝去的感伤
逝去的感伤 2020-12-20 13:12

Imagine you have a file

sink(\"example.txt\")
data.frame(a = runif(10), b = runif(10), c = runif(10))
sink()

and would want to add some hea

8条回答
  •  庸人自扰
    2020-12-20 13:22

    You generally can't expand a file backwards with most filesystems.

    Normally, when you save a file, the existing data is completely overwritten. Even if you only change the first two lines of a 1,000,000 line file, the application will usually re-write the unchanged lines to disk when you hit save.

    For most file formats, any headers are fixed size, so it's not a problem to change them.

    There are also formats that are stream based; since the data is parsed from the stream and used to construct the document, it's possible for the stream to contain an instruction to insert some data at the beginning of the resulting document. These stream-based file formats are fairly complicated, though.

提交回复
热议问题