When using fstream, how can I start outputting in a specific place?

Hello.

I have a ofstream with text already in it. There are for example 2 lines

1
2
START
FINISH


How can I start typing in a specific place? In this case the index would be 1 if I am correct, but how can I put my line in the 1's place (between START and FINISH). Finally it should look like this

1
2
3
START
ASDF
FINISH


Thanks!
You can set the starting position at the end of the file with openmode flags
http://www.cplusplus.com/reference/fstream/ofstream/ofstream/

There is no way that I know of to insert content in the middle of a file. Output operations will replace the data already present. You could implement a function that copies all the data from the starting position to the end in a temporary stream, write the new data (overwriting the previous) and then copy back the rest of the data. To set the position in the file use seekp()
http://www.cplusplus.com/reference/ostream/ostream/seekp/

In this case the index would be 1

No. In that case the index is "after the first line", which means right after the first newline character. To get to a specific line search the the file for the Xth occurrence of '\n', where X is "write after this line", and set the position right after it
Topic archived. No new replies allowed.