为什么fopen(& quot; any_path_name& quot;,' r')不会将NULL作为返回值? [英] Why does fopen("any_path_name",'r') not give NULL as return?

查看:90
本文介绍了为什么fopen(& quot; any_path_name& quot;,' r')不会将NULL作为返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

While debugging some code I got something like below:

#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中输入path of valid directory,它将打印working:

If I give path of valid directory in fopen then it will print working:

如果我在打开时输入path of invalid directory,它将打印it is null

If I give path of invalid directory in fopen then it will print 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标准标准的扩展.

And error code setting is an extansion to ISO C standard standard.

错误也不会在此处设置

#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应该可以在它们上工作.

I think that in Unix everything (directories included) is considered to be file so fopen should work on them.

这篇关于为什么fopen(&amp; quot; any_path_name&amp; quot;,&amp;#39; r&amp;#39;)不会将NULL作为返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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