如果文件已经打开,fopen是否会返回NULL指针? [英] Does fopen return NULL pointer if file is already open?

查看:377
本文介绍了如果文件已经打开,fopen是否会返回NULL指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我假设如果文件已打开,则fopen返回NULL指针.但是如果文件已在"w"模式下打开,则fopen不会返回NULL.下面是我用来尝试此操作的代码,但没有收到任何错误.我尝试使用mingw32以及TDM-GCC-64编译器.如果我没记错的话,如果文件已经打开,C ++就会报错.

I was assuming that fopen returns NULL pointer if file is already open. But it looks fopen does not return NULL in case file is already open in "w" mode. Below is the code that I used to try this and I do not get any errors. I tried with mingw32 as well as TDM-GCC-64 compilers. If I am not mistaken, C++ gives error if file is already open.

#include<stdio.h>

int main()
{
    FILE *fp1, *fp2;
    
    fp1 = fopen("file1.txt", "w");
    fp2 = fopen("file1.txt", "w");
    
    if(fp2 == NULL)
    {
        printf("Error in opening file\n");
        return(0);
    }
    
    // Edit: added following code to check the behavior if write operation
    // is performed simultaneously
    
    fputc('A', fp1);
    fputc('M', fp1);
    fputc('S', fp1);
    fputc('B', fp2);
    
    fclose(fp1);
    fclose(fp2);
    
    return 0;
}

添加了额外的代码以将某些数据写入fp1fp2并查看其行为.如果执行,file1.txt将包含数据BMS,并且似乎是正确的行为,并且fp1fp2会按预期独立移动.首先使用fp1编写AMS,然后使用fp2A替换为B,最终输出为BMS.

Added extra code to write some data to both fp1 and fp2 and see the behavior. If executed, file1.txt contains data BMS and seems to be correct behavior and fp1 and fp2 move independently as expected. First AMS is written using fp1 and then A is replaced by B using fp2 and final output is BMS.

推荐答案

根据C标准(7.19.3.8),它是实现定义的:

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:

某些实现不允许同时打开同一文件的多个副本.因此,可移植代码不能取决于如果违反此规则将发生什么.即使在并非完全无法打开已打开文件的实现中,TOCTOU(检查时间,使用时间)竞争条件也存在,其中第二次打开可能会与第一个打开操作在不同的文件上,这是由于以下原因:文件被移动或删除(请参阅FIO45-C.有关访问TOCTOU竞争条件的更多详细信息,请在访问文件时避免使用TOCTOU竞争条件).

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

这篇关于如果文件已经打开,fopen是否会返回NULL指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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