Sign in to follow this  
Simplicity

Crash between file operations

Recommended Posts

Simplicity    158
Let's say we are using file operations to write debug messages for our program. Say we open a file for writing at the beginning using fopen and write using fwrite and close the file only at exit. Somewhere during the program it crashes and no fclose could be called. What will happen? Will data will be lost, file corrupt, etc?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Quote:
Original post by Simplicity
Let's say we are using file operations to write debug messages for our program. Say we open a file for writing at the beginning using fopen and write using fwrite and close the file only at exit. Somewhere during the program it crashes and no fclose could be called. What will happen? Will data will be lost, file corrupt, etc?




This is an old problem with using the 'print to log' debug methodology.
If the program crashes, the file is usually closed by the OS, but printed text may still have been in the File buffer and may not all have gotten written to the file.

The usual solution is to do a Flush command on the file each time you write one debug message (it will force a write of any buffered data). Depending on the language and OS you may even have to close/reopen the file repeatedly to have the same effect.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this