在调试一些代码时,我得到如下内容:
#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!
Edit:
如果我给path of valid directory
在 fopen 中然后它会打印working
:
如果我给path of invalid directory
在 fopen 中然后它会打印it is null
Edit:规格说
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 应该对它们起作用。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)