0

I currently have to use only linux system calls such as write, read, lseek, open and creat and the C language. I want to insert text into an existing file but whenever I use the write call in the middle of the file it overrides the text in front of it. Is there a way so that write inserts text instead of overriding what is there.

  • What have you tried so far? Show us some code – Daniel Sep 03 '17 at 20:35
  • 1
    No, there is no trivial way to insert an arbitrary-length chunk of data into a file; `write()` (and all other similar functions) will overwrite old data with the new data. You can *append* to a file (and there is even a flag that ensures all writes occur after any existing data). To *insert*, you need to essentially copy the rest of the file contents from the insertion point to after the insertion point. There are two basic ways to do this, depending on how long the inserted data is. (You can use a buffer to read and write it from insertion point on; or you can do it in chunks back-to-front). – Nominal Animal Sep 03 '17 at 20:42

0 Answers0