Search code examples
c++cfilefopenfclose

Can fopen or fstream etc. destroy file by accident C/C++


  1. Let's say I have somewhere in my program a function that opens a file with fopen and then my program does something else (say, try to calculate something) and then it crashes, because of some Null pointer, or stack overflow, or segmentation fault etc. Is there any chance that this would destroy/mess up the file I opened?

  2. What if I opened it for writing. Would it then be even more dangerous?


Solution

    1. If you open a file for reading then no, you cannot corrupt a file.

    2. If you open a file for writing then yes, it is always possible to leave the file in some corrupt state.