Advertisement

Errors in the simple file access example or bug?

Started by July 22, 2000 11:08 PM
3 comments, last by Galileo430 24 years, 5 months ago
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.
------------------------------------------------------------I wrote the best video game ever, then I woke up...
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
Advertisement
Thx, You helped me fix it.
------------------------------------------------------------I wrote the best video game ever, then I woke up...
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;
}
Neo_Matrix
Even though it is better coding practice, the ifstream automatically closes the file once you exit the function you declared it in anyway.

jumble
-----------
Microsoft patents ones and zeroes - remind you of Hasbro?
jumble-----------

This topic is closed to new replies.

Advertisement