Archived

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

Loss of data if file not closed properly...

This topic is 5779 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

Hello For using files in my apps I use the usual FILE structure and functions like fopen, fclose, fread, fwrite and fprintf. Now I have noticed that if you fail to close a file before quiting your App, all the file data is lost. For my terrain engine I am trying to implement background data loading from the hard-disk to support larger maps. This requires me to have the terrain heightmap data file open throughout the execution of the game since calling fopen/fclose every frame is not anyones idea of speed, or memroy management. But this concerns me since, if for whatever reason the program crashes (whether during development or if the user does something silly like pressing the reset button in the middle of the game), all the heightmap data will be erased. Is there anyway around this? are there any "file handling libraries" which prevent this, or perhaps a method around this problem? Thanks in advance.

Share this post


Link to post
Share on other sites
Try:
- C++ std::fstream objects.
or
- Try to open files in read only mode if you don''t need to write. Also you could make some autosave strategy every five minutes or every turn for write, read-write mode opened files.

Share this post


Link to post
Share on other sites
You shouldn''t lose data even if you exit your app without explicitly closing your files. The C runtime should flush and close each output file on exit, unless you call abort() or TerminateProcess() directly (ExitProcess() may also bypass runtime cleanup, but I''m not sure on that). Are you calling one of those, by any chance?

Share this post


Link to post
Share on other sites
My apologies, I should have known: As Pepe correctly mentioned, opening the file in read-only mode is the trick. The file data is not erased even if the file is not closed or flushed.

Thanks again

Share this post


Link to post
Share on other sites