0

Hey,
I was using some code to write int's defined by user to a file but found when you use

myfile.open ("lover_names.txt");
    myfile << firstLover << " " << secondLover << " " << lovePercent << "%\n\n";
    myfile.close();

It replaces the other text in the text file, the answer will most likely be simple but i don't know it, Help ASAP?

3
Contributors
14
Replies
15
Views
9 Years
Discussion Span
Last Post by Black Magic
0

The answer is in the open() function -- see the flags for the second argument. If you want all writes to go at the end of the file the second argument should be ios::ate.

Or you could do seekp() first to set the file pointer to end-of-file. But ios::ate is a lot easier.

0

Thanks but i'm still stuck, where would i put ios::ate?

myfile.open ("lover_names.txt");
    myfile << ios::ate << firstLover << " " << secondLover << " " << lovePercent << "%\n\n";
    myfile.close();

This does the same as before, only keeps one thing in but this time it always has a 2 infront of the name etc, please correct me and show me where to put w.e

0

>> where would i put ios::ate?
You didn't read my post very carefully did you? Or the link I posted ??? myfile.open ("lover_names.txt". ios::ate);

0

also, it might help to have:

myfile.open ("lover_names.txt", ios::ate | ios::app);

So it automaticly seeks the end of the file before you begin writing to it.

0

Sorry i read wrong,

I got errors with your code, it said missing ) before ios, so then i put ("lover...")(ios::..); etc and still got errors,

Then i put..

myfile.open ("lover_names.txt"); ios::ate;
    myfile << firstLover << " " << secondLover << " " << lovePercent << "%\n\n" << ios::ate;
    myfile.close();

But the same thing is happening, no multiple lines etc!?

0

how long have you been programming now? Is this your first semester? Look at what I posted and compare it with what you did. Everybody makes mistakes, but you have to learn to recognize the simple errors you make.

>>But the same thing is happening, no multiple lines etc!?
Of course not because you didn't change the *.exe file. The compiler doesn't replace the *.exe file when there are compile or link errors. There's no point running the old program until all errors are currected.

0

You have to include the parameters in the open() function.
When opening the file just have:

myfile.open ("lover_names.txt", ios::ate | ios::app);

instead of

myfile.open ("lover_names.txt"); ios::ate;

That is all, then you can simply just write to the end of the file by using:

myfile << "Text";
0

Thanks william! Now that bit works, just i still get the "2" on every line, can i get rid of that?

0

@ William - Cool, i wish we could get programming classes at our age at school though :/

0

Yeah ino, I'm learning flash in I.T atm but i know more about computers than my I.T teacher.. I've took something called CIDA for year 10 and thats to do with computers etc :) BUT STILL NO PROGRAMMING LESSONS

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.