Why fopen() doesn’t get fail by passing directory name in argument?
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<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 |
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<stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> int main() { struct stat statbuf; FILE *fb = fopen("/home/jeegar/","r"); if(fb==NULL) printf("its null\n"); else printf("not null\n"); stat("/home/jeegar/", &statbuf); if(S_ISDIR(statbuf.st_mode)) printf("directory\n"); else printf("file\n"); return 0; } |
Output is
not null
directory |
You may like to read this also....
1 Comment to “Why fopen() doesn’t get fail by passing directory name in argument?”
Post comment
Search in this website
our sponsors
latest comments
- shinto peter on How to configure mail from localhost ( wamp ) using PHP?
- tammylleanne on Implementation limitation of c programming language
- Deepak on How to access/unblock songs.pk in india?
- Deepak on How to access/unblock songs.pk in india?
- prk on How to access/unblock songs.pk in india?
Find us on Facebook
Top Authors
Find us on stackoverflow
Polls
Loading ...
My Bookmarks
- Audio/video Recorder & player application based on MATLAB
- check dependency of your binary
- defination of all standard c programming language function
- Great Question-Answer on c programming
- know what your c code means
- Limition of c programming language
- List of all version of c programming language
- Online c compiler
- php freelancing work
- some more stuff on C programming language
- Volatile Keyword in Embedded System
- Write Android application in c language
Like the blog