gpt4 book ai didi

c - 为什么 fopen ("any_path_name",'r' ) 不返回 NULL?

转载 作者:太空狗 更新时间:2023-10-29 11:39:27 27 4
gpt4 key购买 nike

在调试一些代码时,我得到如下内容:

#include<stdio.h>

int main()
{
FILE *fb = fopen("/home/jeegar/","r");
if(NULL == fb)
printf("it is null");
else
printf("working");
}

在 fopen 中,我给出了一个有点有效的路径名,但不是文件名。那么 fopen 不应该返回 NULL 吗?但它不会返回 null!

编辑:

如果我在 fopen 中提供有效目录的路径,那么它将打印working:

如果我在 fopen 中给出 无效目录的路径 那么它会打印 it is null

编辑:规范说

Upon successful completion, fopen() shall return a pointer to the object 
controlling the stream. Otherwise, a null pointer shall be returned.

所以这里无论是否设置错误码,都必须返回NULL

错误码设置是对ISO C标准标准的扩展。

错误也不会在这里设置

#include<stdio.h>
#include <errno.h>

int main()
{
errno = 0;
FILE *fb = fopen("/home/jeegar/","r");
if(fb==NULL)
printf("its null");
else
printf("working");


printf("Error %d \n", errno);


}

输出是

workingError 0 

最佳答案

我认为在 Unix 中所有东西(包括目录)都被认为是文件,所以 fopen 应该对它们起作用。

关于c - 为什么 fopen ("any_path_name",'r' ) 不返回 NULL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8399475/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com