I have a simple program running with a logger file open throughout the duration of the process. I researched online and it said that files automatically close when the exit() function is called or the main function returns. However, it says nothing about if a program crashes during execution.
Therefore, is there a way I can manually, or even automatically, close a file after the program crashes?
Modern operating systems will automatically clean up OS resources like files and allocated memory, so the file will be closed after a crash like this. However if using standard library I/O (FILE* or even C low-level file descriptors) there may be buffered writes that are not yet part of the operating system's view of the file. Those unbuffered writes are most likely simply gone.
However, the more generally accepted correct answer is to code in such a way that you will not have such exceptions to begin with.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With