When i open file for output it wipes erases what's in there

hey everyone, i am ready to throw my computer at a wall for this.

So if you want to help me save my sanity and a few hundred bucks in a new computer, please assist me on this one.

Basically, the main problem is that when i open a txt file for output to write a simple message to it, it wipes out the contents in it and does not write the message to it. Also, i don't get an error message when checking if the output file was opened correctly, so i assume it was opened correctly(?), it

I am also reading input for this program, but that part is working well. Maybe the way i did this is affecting the output part??

ifstream input("quadratic.txt");
if (input.fail())
{
cout<<"Error opening the file for input";
return 0;
}
ofstream outfile;
outfile.open("IamME.txt");

if (outfile.fail())
{
cout << "Error opening file for output";
}
outfile<<"THIS IS A LINE OF COKE";


Any help would be greatly appreciated
Last edited on
I really don't care about the data in the txt file being erased when i run the program, i just want

to put data in it but i am unable to do that.
Last edited on
You might want to take a look at this page:

http://www.cplusplus.com/reference/iostream/ifstream/open/

Basically you can open files in various modes and it sounds like the default action for open() that you are using is write mode. You probably want APPEND mode which will append to the file instead of clearing it before writing to it, which is what standard write mode does =)

Try this...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
ifstream input("quadratic.txt");
if (input.fail())
{
cout<<"Error opening the file for input";
return 0;
}
ofstream outfile;
outfile.open("IamME.txt", ifstream::app);

if (outfile.fail())
{
cout << "Error opening file for output";
}
outfile<<"THIS IS A LINE OF COKE";
Thankyou, it turned out i only needed to write

outfile.close();
Ahh ok. Glad you got it working :)
Topic archived. No new replies allowed.