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?

Dani AI

Generated

Brief diagnosis and what caused the mysterious "2"

  • The file was being opened with the default output mode, which replaces the file contents. The correct place for open-mode flags is the file open call; streaming an open-mode object into the file with operator<< will print its numeric value — that is why saw a leading "2" on each line. was right to point at the open call, and was right that combining modes can be useful.

Clarification of the common modes (so the choice is clear)

  • One open-mode moves the file position to the end once right after opening (initial seek) but still allows repositioning later. The other forces every write to happen at the file end (append behavior), so writes cannot accidentally overwrite earlier data. Also note that many ofstream constructors/open calls use truncation by default unless an append-like mode is explicitly requested. The reference pages explain the modes and default behavior in detail: std::ios_base::openmode and std::basic_fstream::open.

Quick troubleshooting checklist (practical)

  • Rebuild after fixing any compile/link errors; running the old executable can mask fixes.
  • Check the stream state after opening (is_open() or testing the stream) before writing.
  • Remove any operator<< calls that stream an open-mode constant; only stream text/data.
  • Choose the append-style mode when the intention is always to add new lines, or choose the initial-seek mode if occasional repositioning is needed.
  • If a stray numeric character still appears, search for accidental prints of integers/flags or the wrong variable being streamed.

Recommended Answers

All 14 Replies

The answer is in the -- 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.