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

标签 c linux file gcc fopen

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

#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/

相关文章:

c - 标准c库是否提供链表等数据结构?

linux - apache mod_rewrite mod_proxy 子域到目录

linux - Samba 导致 "mount:/is busy"

python - 在 python 中使用 file.seek() 时通常将多少字节加载到内存中?

c - 在给定的用户定义函数中,求值顺序是否会无法将源字符串复制到目标字符串?

c - strcpy c 函数不起作用 IOS7 的 IOS C 自定义库

c - C语言中如何截去小数而不进行四舍五入

linux - wget 如何只保存从目标页面链接到的页面链接到的某些文件类型?

python - 如何使用Python的PID查看Python正在读取或编辑哪些文件?

java - 如何读取文件 : Properties > Details > Content > Pages e. g。对于word文档