FCLOSE一个文件,该文件是已经FCLOSE [英] Fclose a file that is already fclose

查看:233
本文介绍了FCLOSE一个文件,该文件是已经FCLOSE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我PROGRAMM我可以关闭一个文件,是已经接近。什么时候我在文件上做了 FCLOSE 发生已经接近?

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 ?

推荐答案

调用 FCLOSE 具有相同的流两次是未定义的行为 - 最有可能崩溃。有没有办法检查 FILE * 已经已经关闭,所以安全的解决方案是,只要它是封闭的指针设置为NULL:

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;

来源:一个指向一个文件对象的值是不确定的相关文件关闭后(C标准草案)。 的号召,FCLOSE()后,任何使用流会导致未定义的行为。 (单一UNIX®规格)。

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).

这篇关于FCLOSE一个文件,该文件是已经FCLOSE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆