Add new line to end of txt file

Jun 9, 2008 at 10:08pm
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.
Jun 9, 2008 at 11:32pm
You should be able to open the file in append mode.
Check out the mode flags for open() (they are the same as those used by the fstream constructors).

Have fun!
Jun 10, 2008 at 12:18am
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>

#include <sstream>
#include <cstring>
using namespace std;

int main(int argc, char *argv[])
{
    fstream filestr;

  filestr.open ("file.txt", fstream::in | fstream::out | fstream::app);

  filestr<<"YAY"<<endl;


    
    system("PAUSE");
    return EXIT_SUCCESS;
}
Last edited on Jun 10, 2008 at 12:24am
Jun 10, 2008 at 12:24am
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

Did I help?
Last edited on Jun 10, 2008 at 12:26am
Jun 10, 2008 at 12:32am
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...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>

#include <sstream>
#include <cstring>
using namespace std;

int main(int argc, char *argv[])
{
    fstream filestr;

  filestr.open ("file.txt", fstream::in | fstream::out | fstream::app);

  filestr<<"YAY"<<endl;
filestr.close();
    system("PAUSE");
    return EXIT_SUCCESS;
}


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...
Last edited on Jun 10, 2008 at 12:36am
Jun 10, 2008 at 12:44am
What you have posted works fine for me...

You can't write to an istream, and you can't read from an ostream. So if you only specify ios::in then writing should not work.

Hope this helps.
Jun 10, 2008 at 1:02am
Ok thank you for your help.

I'm running Vista... so I'm gonna blame it on that. I'll try on an XP machine 2mw
Jun 10, 2008 at 1:24am
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...
Topic archived. No new replies allowed.