3

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 
Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222

4 Answers4

3

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

PhoenixS
  • 1,016
  • 1
  • 9
  • 23
  • and as shk said - if you fopen directory errno is set to EISDIR [source](http://pubs.opengroup.org/onlinepubs/009695399/functions/fopen.html) – PhoenixS Dec 06 '11 at 12:01
  • About checking errno: I think that simply checking `errno` variable should be [ok](http://publib.boulder.ibm.com/infocenter/iadthelp/v7r5/index.jsp?topic=/com.ibm.etools.iseries.pgmgd.doc/cpprog416.htm) – PhoenixS Dec 06 '11 at 12:07
  • 3
    `EISDIR` is only returned if the operation also requires write access, per that source. Also, you should edit your answer rather than post a comment to it. – Potatoswatter Dec 06 '11 at 12:22
  • Thanks still learning - will do next time posting something correct :-) – PhoenixS Dec 06 '11 at 12:27
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.

rodrigo
  • 94,151
  • 12
  • 143
  • 190
1

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!

another.anon.coward
  • 11,087
  • 1
  • 32
  • 38
-1

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.

0xAX
  • 20,957
  • 26
  • 117
  • 206