文件关闭后FILE指针会发生什么变化?

发布于 2024-12-20 11:21:09 字数 181 浏览 1 评论 0原文

我想知道文件关闭后 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

阪姬 2024-12-27 11:21:09

由于参数是按值传递的,因此 fclose 无法将文件指针设置为 NULL。由于fclose可能会破坏FILE,因此您必须

  • 在执行fclose后手动将文件指针设置为NULL(赢得了如果您在不同的函数中关闭它,则不起作用,除非您使用 FILE **
  • 不要最终陷入“忘记”是否关闭它或不(可能很棘手)

Since arguments are passed by value there is not way fclose could set your file pointer to NULL. Since fclose probably destroys the FILE you have to

  • Manually set the file pointer to NULL after doing a fclose (won't work if you close it in a different function unles you use FILE **)
  • Don't end up in a situation where you "forget" whether you closed it or not (might be tricky)
她如夕阳 2024-12-27 11:21:09

Peter Norvig 引用 Auguste Comte (1798-1857):

  "Nothing is destroyed until it is replaced"

您可以使用宏:

  #define fclose(fp)  ((fp) ? fclose(fp) : 0, (fp) = 0)

这修复了两个不同且相反的问题:

  • 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):

  "Nothing is destroyed until it is replaced"

You could use the macro:

  #define fclose(fp)  ((fp) ? fclose(fp) : 0, (fp) = 0)

This fixes two different and opposing problems:

  • The FILE * pointer is NULL'd after fclose, so it can't be
    fclose'd twice.

  • This version of fclose will accept a NULL argument. Many common
    versions of fclose--such as those in HPUX, SGI, and CYGWIN--are happy
    with 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 it FCLOSE, to follow convention.

苦妄 2024-12-27 11:21:09

FILE * 它是指向 FILE 结构的指针,当您调用 fclose() 时,它将销毁/释放 FILE 结构,但不会更改 FILE* 指针的值,这意味着它仍然具有该 FILE 结构的地址,该地址现在不存在。

同样的事情发生在任何使用 malloc 获取的指针上

int a malloc(10);
free(a);

仍然不会是 NULL

在大多数情况下我总是这样做

free(a);
a=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

int a malloc(10);
free(a);

still a will not be NULL

in most case i always do this things

free(a);
a=NULL;

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

一张白纸 2024-12-27 11:21:09

是的,你可以这样做。更好的方法是将 fp 设置为 NULL
否则

它将是悬空指针(指向它不拥有或不存在的东西的指针)

Yes you can do this. and better way is to set fp to NULL
else

it will be dangling pointer (a pointer which is pointing to something which it does not own or which does not exist)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文