First, for reading one character at a time, streaming from cin isn't useful since streaming requires a press of return every time to complete the input. It would be much easier to use istream::getline()
Second, if you wish to append text to an existing file, you have to open it for appending; see fstream::open()
. You omitted the mode, so you're replacing (overwriting) the file every time.
Third, IO is slow: instead of writing the stuff you wish to insert to file after every bit of text, it would be better to assemble the entire text in memory before even opening the output file. What you're doing is like an editor that saves the entire file after every single word you type. That is not very sensible for multiple reasons, most importantly you needlessly copy the text that's already in there over and over again.