I have a text file where each line is terminated by a "\n". Like so
0000
0000
0000
0000
Now, a file initially starts like above but a line can be overwritten with another line of variable length. I am using fseek()
in order to set the position indicator of the stream. I have no problems moving to a line and replacing for example 0000
with 1111
.
Now lets say I want to replace the first line with 1111 1111
. I do the following
char *str = "1111 1111\n";
fwrite(str, 1, strlen(str), file);
This deletes line 2 and 1 byte from line 3 like so
1111 1111
000
0000
What is the cause of this?
If you think of your file like: "0000\n0000\n0000\n0000\n"
It will be easier to understand that when you overwrite first line you are overwriting first n-number of chars really. So that is why your second line disappears.