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)
Printable View
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)
#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;
}
It works! Thanks so berry much :D
Ummmm...see the corrections ;)Quote:
Originally posted by SteveCRM
Code:#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;
}