Hi everybody. Does anybody know of a way to insert a line of text in the begging of the textfile without having to read an entire file in a loop? Preferably using ifstream? Thank you for the replies.
Hi everybody. Does anybody know of a way to insert a line of text in the begging of the textfile without having to read an entire file in a loop? Preferably using ifstream? Thank you for the replies.
You cannot do what I think you want with fstream. I'm not sure you could even do it even with platform specific file i/o functions.
However, you can do it without using a loop. Just read the entire contents of the file into a string via a stringstream, then output the new first line followed by that string.Code:ostringstream ostr; ostr << in_file.rd_buf(); out_file << "this is the new first line.\n" << ostr.str();