How to insert a line between two lines in a file?

Hi everyone,

I am currently writing a program that replaces, deletes and inserts lines into a file. The program outputs line by line to the user and they can specify when they want to replace, delete or insert a line. I have the replace and delete functions completed but unsure how to approach the insertion.

Any help or hints to get me started would be greatly appreciated!

Thank you!
Can you show us the code you have?

Are you using a STL container to store the lines? If so you could use one with insert such as a list.

Otherwise basically inserting just copys and moves as far as I am aware.
I actually solved it, this is what I came up with. I used a temp file to store the remainder of the code, wrote the user written line to the end of the original file and then rewrote the lines from temp file back to the original file (which was appended so it would write at the end and not overwrite existing lines).

However, I have a new question now. On line 21 I don't want it to reset back to the beginning of the original file, I want it to reset back to the line the user left off at (or maybe the newly inserted line). Is there a way to do this, to get the position of the line the user left off on?

Here's my reset functions, pretty simple:

1
2
3
4
5
void Modify::reset(istream& file)
{
    file.clear();
    file.seekg(0, ios::beg);
}

Last edited on
Topic archived. No new replies allowed.