Why fopen() doesn’t get fail by passing directory name in argument?

By -

Once i have made one small application where i was passing full file name as command line argument in my application where i am going to fopen() that file BUT once i have just passed any directory name instad of file name still my fopen() does not get fails…!!

#include <errno.h>
int main()
errno = 0;
FILE *fb = fopen("/home/jeegar/","r");
    printf("its null");
printf("Error %d \n", errno);

output is

 workingError 0

here fb is not going null and also not getting any error..

after some research i came to know that in

“In Linux everything (directories included) is considered to be file”

so opening any directory in read mode is not getting fail but if you open directory in write mode then fopen will get fail.

So now Question comes :

how can we check that fopen() has opened that is file or directory?

here is some code which shows you trick for checking that things

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int main()
struct stat statbuf;
FILE *fb = fopen("/home/jeegar/","r");
    printf("its null\n");
    printf("not null\n");
stat("/home/jeegar/", &statbuf);
return 0;

Output is

 not null

hey I am an Artist who love to write code...! Well I am an EC graduate From Ganpat University and now i am working as Embedded software engineer in one private firm.. find me at here Jeegar Patel

1 Comment to Why fopen() doesn’t get fail by passing directory name in argument?

Leave a Reply

Your email address will not be published. Required fields are marked *