[Solved] C++ append to existing file [duplicate]


You can’t. Files don’t really have lines, they just store a bunch of characters/binary data. When you have

1,2,3,4,5
6,7,8,9,0

It only looks that was because there is an invisible character in there that tells it to write the second line to the second line. The actual data in the file is

1,2,3,4,5\n6,7,8,9,0

So you can see then end of the file is after the 0 and to get after the 5 you would need to seek into the middle of the file.

The way you can get around this is to read each line of the file into some container and then add your data to the end of each line. Then you would write that whole thing back o the file replacing the original contents.

solved C++ append to existing file [duplicate]