Archived

This topic is now archived and is closed to further replies.

Galileo430

Errors in the simple file access example or bug?

Recommended Posts

Galileo430    122
Everytime I write out the program and run it I get this error. First-chance exception in filetest.exe: 0xC0000005: Access Violation. I checked and the file is not write protected. Here is the source, #include #include int main(int argc, char **argv) { int a; char *line; ifstream fin("output.txt"); //Address of a file the says "Hello World" fin >> line; cout << line << endl; //Output Filedata cin >> a; //Hold here return 0; } Thx for the help.

Share this post


Link to post
Share on other sites
Nate    122
I don''t use ifstream much, but I think your problem is that ''line'' doesn''t point to any memory. Try ''char *line = new char[255];'' and see if that fixes things.

Nate Miller
http://nate.scuzzy.net

Share this post


Link to post
Share on other sites
Neo_Matrix    122
Also you should really close the file stream. By adding the following line when done writing to the file:

[SOURCE]
#include
#include

int main(int argc, char **argv)
{
int a;
''char *line = new char[255];

// If open the stream then close it
ifstream fin("output.txt"); //Address of a file the says "Hello World"
fin >> line;
fin.close(); // Close the stream, better programming practice
cout << line << endl; //Output Filedata
cin >> a; //Hold here

return 0;
}

Share this post


Link to post
Share on other sites