I would like to overwrite a certain line in a txt file while keeping all others the same. Is there a nice and simple way of doing this?
-
Possible duplicate of [C write in the middle of a binary file without overwriting any existing content](http://stackoverflow.com/questions/10467711/c-write-in-the-middle-of-a-binary-file-without-overwriting-any-existing-content). Although for binary files and pure insertion, the concepts are substantially the same for an overwrite; you just move the data slightly differently. – Jonathan Leffler Apr 01 '14 at 23:27
2 Answers
Basicly use Fseek() to search the sequence of bits that you want to change,
then use Fwrite() to overwrite the old text
OBS: you need to open the file in rw mode to overwrite
fopen ("myfile.txt", "wr");
use those referenses:
http://www.cplusplus.com/reference/cstdio/fwrite/
http://www.cplusplus.com/reference/cstdio/fseek/
(it says C++ but works on C)

- 2,294
- 2
- 16
- 22
-
1As noted, this will only work where there are the same number of old and new bytes being written. – Alex Reynolds Apr 01 '14 at 23:17
-
1Also, be careful when using `fseek` on text files, as the seek offset may not equal the number of bytes read from the start to reach that point. – M.M Apr 02 '14 at 00:57
Unless the length of the line you are overwriting has the same number of bytes, you can't "insert" or "remove" bytes into or from an existing file. You would have to write a new file:
- Read in all the old lines up to the one you need to overwrite, and write them to another output stream (either a new file pointer or
stdout
) - Write out your new line to the output stream
- Read in all the old lines after the line you overwrite, and write those lines to the new output stream
If you want to overwrite existing bytes and you know for sure that the lengths of the old and new lines are exactly equivalent, then you can:
fopen()
the file inrw
modefseek()
to the byte position of the old line (or read in characters until you hit some preset number of newline characters, etc. — basically, you want to move the file pointer to the start of the old line)fwrite()
new bytes over the old line's bytesfclose()
the file pointer
If you really need to overwrite bytes in the same file and your new line has fewer bytes than the old line, you could perhaps do some tricks where you overwrite the end of the old line with space characters up to the newline character, but the cleaner solution is to simply write a new file with the updated content.

- 95,983
- 54
- 240
- 345
-
Damn, that's what I thought I'd have to do in the first place. I thought maybe someone had a trick out there. Thanks – ScarletSickle Apr 01 '14 at 23:13
-
But if you read the old lines that are after the new line after overwriting, you might not had all the old lines. It's better to read the whole file before, write the old lines before the new line, write the new line, then write the rest of the old lines, no? – Biduleohm Apr 01 '14 at 23:15
-
P.S. could you point me to a snippet of code that does something similar. I'm not very good with C I/O – ScarletSickle Apr 01 '14 at 23:17
-
Just use a search engine on terms like `fseek`, `fwrite`, `code sample` etc. There are many examples already out on the web. – Alex Reynolds Apr 01 '14 at 23:18