Fclose a file that is already fclose

ε祈祈猫儿з 提交于 2019-12-29 07:49:50

问题


In my programm I may close a file that is already close. What happen when I do a fclose on a file already close ?

And if you can't do so, how to know if a file is closed or open ?


回答1:


Calling fclose twice with the same stream is undefined behaviour - most likely crash. There is no way to check if FILE* has been closed already, so the safe solution is to set pointer to NULL as soon as it is closed:

fclose(fh);
fh = NULL;

Sources: "The value of a pointer to a FILE object is indeterminate after the associated file is closed" (C draft standard). "After the call to fclose(), any use of stream causes undefined behaviour." (The Single UNIX ® Specification).



来源:https://stackoverflow.com/questions/24555980/fclose-a-file-that-is-already-fclose

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!