Why does fopen(“any_path_name”,'r') not give NULL as return?

守給你的承諾、 提交于 2020-01-04 06:52:11

问题


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 

回答1:


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




回答2:


The posix man page man 3p fopen says, in the section ERRORS:

The fopen() function shall fail if:

[...]

EISDIR The named file is a directory and mode requires write access.

(Emphasis mine). Since you are not requesting write access, and chances are that the path you use is a directory, the function does not fail.

About what can you use with a FILE* that refers to a directory, I have no idea.




回答3:


As you might be very well aware that pretty much everything on Linux system is a file, if not a file then its a process (corrections & remarks welcome :) ) Directory is treated like a file which lists other files (Reference from TLDP); so opening to read a directory as a file is a valid operation and thus you do not get any error. Although trying to write to it is not allowed, so if you open directory in write or append mode, the fopen operation will fail (this has been very well mentioned is other responses & link to fopen documentation). Most of the file operation like read & write operations on this file stream will fail with the error stating that its a directory. Only use which could be found was finding the size of the file (directory in this case) using fseek to SEEK_END & ftell (which will most likely give a result of 4096).
Regarding using errno to get meaningful messages, you can use perror which is in stdio.h & pass message which will be added before the error message or strerror which is in string.h & pass errno which is in errno.h
Hope this helps!




回答4:


How to check that errno?

You can check errno for example:

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

int main(void)
{
   FILE *fp;
   errno = 0;
   fp = fopen("file.txt", "r");
   if ( errno != 0 ) 
   {
     // Here you can check your error types:
     perror("Error %d \n", errno);
     exit(1);
   }
}

Error types you can find at http://pubs.opengroup.org/onlinepubs/009695399/functions/fopen.html Error section.



来源:https://stackoverflow.com/questions/8399475/why-does-fopenany-path-name-r-not-give-null-as-return

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!