Jump to content
  • Advertisement

Archived

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

Galileo430

Errors in the simple file access example or bug?

This topic is 6542 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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
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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!