为什么fopen(" any_path_name",“r”)不能返回NULL? [英] Why does fopen("any_path_name",'r') not give NULL as return?

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

问题描述

  #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中给出路径无效的目录,那么它会打印它是空的 p>

编辑
规格说明

 成功完成后,fopen()将返回一个指向控制流的对象
的指针。否则,将返回一个空指针。

所以这里是否设置错误代码,它必须返回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 \\\
,errno);


$ b $ / code $ / pre
$ b $ p OUTPUT IS

  workingError 0 


解决方案

我认为,在Unix中,一切(包括目录)被认为是文件,所以fopen应该在他们身上。


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");
}

Here in fopen I gave a somewhat valid path name but not a filename. Shouldn't fopen return NULL then? But it does not return null!

Edit:

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

If I give path of invalid directory in fopen then it will print it is null

Edit: spec says

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

so here whether error code set or not, it MUST return NULL

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

ERROR IS ALSO NOT GOING TO SET HERE

#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);


}

OUTPUT IS

workingError 0 

解决方案

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

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

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