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?

Recommended Answers

All 14 Replies

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.

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

>> 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);

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.

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!?

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.

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";

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

take that ios::ate off the end of line 2 that you posted.

Thank you all, sorry that i did not do as much as i could, sorry :(

No problem, just found out we are both 14 years old. =)

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

Yeah, I know, its really does suck.
All they attempt to teach us is word, excel and publisher..:angry:

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.