文件关闭后FILE指针会发生什么变化?
我想知道文件关闭后 FILE 指针会发生什么。会是NULL吗?
基本上,我想在关闭文件之前检查文件是否已经关闭。
例如如下:
FILE *f;
if(f!=NULL)
{
fclose(f);
}
我可以这样做还是有其他方法可以解决这个问题?
I wish to know what happens to FILE pointer after the file is closed. Will it be NULL?
Basically, I want to check if a file has already been closed before closing a file.
For example as follows:
FILE *f;
if(f!=NULL)
{
fclose(f);
}
Can I do this or is there any other way to go about it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于参数是按值传递的,因此
fclose
无法将文件指针设置为NULL
。由于fclose
可能会破坏FILE
,因此您必须fclose
后手动将文件指针设置为NULL
(赢得了如果您在不同的函数中关闭它,则不起作用,除非您使用FILE **
)Since arguments are passed by value there is not way
fclose
could set your file pointer toNULL
. Sincefclose
probably destroys theFILE
you have toNULL
after doing afclose
(won't work if you close it in a different function unles you useFILE **
)Peter Norvig 引用 Auguste Comte (1798-1857):
您可以使用宏:
这修复了两个不同且相反的问题:
FILE *
指针在fclose
之后为 NULL,因此不能fclose'd
两次。此版本的
fclose
将接受 NULL 参数。很多常见的fclose
的版本(例如 HPUX、SGI 和 CYGWIN 中的版本)很满意与 NULL。奇怪的是,受 FreeBSD 启发的版本(例如 Linux),
而 Microsoft 则不然。
当然,该宏引入了它自己的问题:
它没有返回正确的错误值。但如果你想看
为此,您可以使用额外的括号禁用宏,如下所示:
if
((fclose)(fp) == EOF){ /* 处理错误... */ }
它没有函数语义,因为它使用其参数
多次。但很难想象这会造成问题。但您可以使用
(fclose)
。或者将其命名为FCLOSE
,以遵循约定。Peter Norvig quotes Auguste Comte (1798-1857):
You could use the macro:
This fixes two different and opposing problems:
The
FILE *
pointer is NULL'd afterfclose
, so it can't befclose'd
twice.This version of
fclose
will accept a NULL argument. Many commonversions of
fclose
--such as those in HPUX, SGI, and CYGWIN--are happywith NULLs. It is odd that the FreeBSD-inspired versions such as in Linux,
and Microsoft, aren't.
Of course, the macro introduces its own problems:
It doesn't return the proper error value. But if you wanted to see
this, you can disable the macro with extra parentheses, as in:
if
((fclose)(fp) == EOF){ /* handle error... */ }
It doesn't have function semantics, as it uses its argument
multiple times. But it is hard to imagine this causing a problem. But you can use
(fclose)
. Or name itFCLOSE
, to follow convention.FILE * 它是指向 FILE 结构的指针,当您调用 fclose() 时,它将销毁/释放 FILE 结构,但不会更改 FILE* 指针的值,这意味着它仍然具有该 FILE 结构的地址,该地址现在不存在。
同样的事情发生在任何使用 malloc 获取的指针上
仍然不会是 NULL
在大多数情况下我总是这样做
编辑:你不能在任何时候检查它是否关闭/释放。只是为了确保您可以在 free/fclose 之后将其分配为 NULL,以便您可以检查其是否为 NULL 并继续..
FILE * It's a pointer to a FILE structure, when you call fclose() it will destroy/free FILE structure but will not change the value of FILE* pointer means still it has the address of that FILE structure which is now not exits.
same things happem with any pointer getting with malloc
still a will not be NULL
in most case i always do this things
Edit: you can not check whether its CLOSED/freed at any time. just to make sure you can assign it NULL after free/fclose so you can check its NULL or not and go ahead ..
是的,你可以这样做。更好的方法是将
fp
设置为NULL
否则
它将是悬空指针(指向它不拥有或不存在的东西的指针)
Yes you can do this. and better way is to set
fp
toNULL
else
it will be dangling pointer (a pointer which is pointing to something which it does not own or which does not exist)