What happens to FILE pointer after file is closed?

前端 未结 4 1644
天涯浪人
天涯浪人 2020-12-06 01:48

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

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-06 02:20

    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.

提交回复
热议问题