How do I add a new line to the end of a text file? The line contains both string literals and variables. I'm sure the syntax is really simple, but I've looked stuff up about seekp/g and tellp/g and it doens't work...
The file itself contains information already, and this information still must remain there... I just want a NEW line added to it.
BONUS: Add multiple lines... ooo..
This is for an assignment, so I would like to try to understand it. As simple as possible.
Can anyone provide me with the syntax?
So far I have the following code, and the file (already containing data) does not have the "text" string being added.
You need to << flush or close() the file before you will see the stuff you wrote.
Otherwise you've got it exactly. :-)
[edit] << endl will flush it also. ;-)
[edit2] It bugs the heck out of me when I haven't even finished asking the question (final edits, etc) and some yahoo is busy answering already...
:-P
This is my code now (just added the close line) ... it still doesn't work... The whole project (all parts) is on the desktop, with the text file named "file.txt" no caps...
If the line filestr.open ("file.txt", fstream::in | fstream::out | fstream::app); is changed to just filestr.open ("file.txt", fstream::in ), the YAY literal is printed in the txt file, but it just replaces the first 4 letters of the file...
If you want just t write at the end of the file try to open it using the fstream::ate parameter that automatically takes the pointer to the end of the file so the next operation is gonna performed there...
And as you posted it it works fine with me. and i also run vista.
I hope that helps...