PDA

Click to See Complete Forum and Search --> : Writing to a file (Im a newbie :P)


Evil_Cowgod
Jul 15th, 2002, 05:14 AM
Well Im new to C++ and I am using "bloodShed Dev C++" as my compiler. I would like to write to a text file "C:\anser.txt" but the I cant get the code I was given to work :(

Could someone please tell me how to write to a text file? :o)

SteveCRM
Jul 15th, 2002, 09:28 AM
#include <fstream.h>
#include <stdlib.h>
#include <iostream.h>

int main()
{
ofstream outfilie("answer.txt", ios::trunc);
if(outfile.fail())
{
cout<<"File Error"<<endl;
exit(1);
}
outfile<<"Test"<<endl;

return 0;
}

Evil_Cowgod
Jul 15th, 2002, 03:56 PM
It works! Thanks so berry much :D

parksie
Jul 16th, 2002, 12:00 PM
Originally posted by SteveCRM
#include <iostream>
#include <fstream>

using namespace std;

int main() {
ofstream outfile("answer.txt", ios::trunc);

if(!outfile) {
cerr << "File Error" << endl;
return -1; // or 1 or something non-zero
}

outfile << "Test" << endl;
} Ummmm...see the corrections ;)