Does fopen return NULL pointer if file is already open?

前端 未结 1 698
情深已故
情深已故 2021-01-27 06:02

I was assuming that fopen returns NULL pointer if file is already open. But it looks fopen does not return NULL in case file

相关标签:
1条回答
  • 2021-01-27 06:32

    According to the C Standard (7.19.3.8), it is implementation-defined:

    Functions that open additional (nontemporary) files require a file name, which is a string. The rules for composing valid file names are implementation-defined. Whether the same file can be simultaneously open multiple times is also implementation-defined.

    On top of that, it is discouraged for other reasons, see for instance SEI CERT C Coding Standard's FIO24-C recommendation:

    Some implementations do not allow multiple copies of the same file to be open at the same time. Consequently, portable code cannot depend on what will happen if this rule is violated. Even on implementations that do not outright fail to open an already-opened file, a TOCTOU (time-of-check, time-of-use) race condition exists in which the second open could operate on a different file from the first due to the file being moved or deleted (see FIO45-C. Avoid TOCTOU race conditions while accessing files for more details on TOCTOU race conditions).

    0 讨论(0)
提交回复
热议问题