Can you check this code

DarkKnight

Golden Member
Apr 21, 2001
1,197
0
0
I just want to input text inot a text file, but I always get errors. Can you tell me whats wrong with the code?

#include <iostream.h>
#include <fstream.h>

const char* file = "C:\\textfile.TXT";
ofstream fileopen(file);

int main()
{
fileopen.open();
fileopen <<"hi" << endl;
fileopen.close();

return 0;
}
 

RudeBoie

Platinum Member
Feb 28, 2000
2,017
0
0
I don't have my compiler on me, so you should really copy and paste the errors if you can.
 

MGMorden

Diamond Member
Jul 4, 2000
3,348
0
76
If you're going to fine file like this, put in the '\0' so it terminates, like this:

const char* file = "C:\\textfile.TXT\0";

Also, if you're hard coding in the file name, why don't you just do

ofstream fileopen ("C:\\textfile.TXT");

and forget about the string (just wasting memory). And even though this isn't an error persay, you really whould avoid using global variables when they aren't necessary (and they usually are not).
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Just remove this line: fileopen.open();

You already opened it before main.

MGMorden,
The explicit NULL terminator is not neccessary. It's automatically placed there for the string.