Programming in C - Determining if a file exists...

Associate
Joined
27 Dec 2004
Posts
612
Location
Andover
Just wondering if anyone has an idea how to do this, i've created a simple program to read line and word counts from a text file in C. The program asks what file you would like to read from and continues from there, my problem is if i enter a file that doesn't exist it seg faults.

Does anyone know a way to test if a file name a user inputs exists using only stdio.h before it tries to open it?
 
Code:
#include <stdio.h>

int main(int argc,char* argv[])
{
      FILE *f;

      if((f = fopen(argv[1],"rt")) == NULL)
      {
            printf("File does not exist on specified directory.\n");
            exit(123);
      }

      return 0;
}
 
Last edited:
Mr Sniper said:
Just wondering if anyone has an idea how to do this, i've created a simple program to read line and word counts from a text file in C. The program asks what file you would like to read from and continues from there, my problem is if i enter a file that doesn't exist it seg faults.

Does anyone know a way to test if a file name a user inputs exists using only stdio.h before it tries to open it?

There's no foolproof way using ISO C, but the method posted by Una iss your best bet. The downside is that it checks if the file can be read. The file may exist but you may not have permission to read it, for example.
 
out of curiosity, how portable is this?
Code:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>
int main(int argc, char *argv[]) {
        int f, errv;
        struct stat sb;
        FILE *fd;
        char *filename = argv[1];
        f = stat(filename, &sb);
        if (f == 0) {
                printf("File exists, ");
                fd = fopen(filename, "r");
                if (!fd) {
                        errv = errno;
                        printf("but %s\n", strerror(errv));
                } else
                        printf("and readable\n");
        } else
                puts(strerror(errno));
        return 0;
}

$ ./mytest doesntexist
No such file or directory
$ touch doesntexist
$ ./mytest doesntexist
File exists, and readable
$ chmod 0 doesntexist
$ ./mytest doesntexist
File exists, but Permission denied
 
Well it would work on any OS that conforms to posix standards so pretty portable aye.
 
Una said:
Well it would work on any OS that conforms to posix standards so pretty portable aye.

fantastic statement :D

what about the other 80% of computers on the planet?



yes I know the number might be wrong, I made it up on the spot...
 
Visage said:
And how many OS's conform to posix standards? ;)

* A/UX
* BlagOS
* BSD/OS [4]
* Cygwin - enables POSIX compliance for certain Microsoft Windows products.
* AIX
* HP-UX
* velOSity
* INTEGRITY
* Irix
* LynxOS
* Mac OS X
* Microsoft Windows Services for UNIX 3.5 - enables POSIX compliance for certain Microsoft Windows products.
* MINIX
* OpenVMS
* penOS
* QNX
* RTEMS (POSIX 1003.1-2003 Profile 52)
* Solaris
o OpenSolaris
* UnixWare
* VxWorks
 
Visage said:
There's quite a popular one missing from that list ;)

is it certified by the open org / IEEE or whoever manages POSIX compliance nowadays? Yes I know it IS POSIX compliant but it isn't certified to be POSIX compliant which is all that matters when it comes to POSIX compliance. Basically did you pay enough money to get the stamp?

Personally I'm amazed that Irix ever passed, it has to have been the nastiest piece of poop that I have ever had to deal with (apart from HP-UX obviously)

Paul
 
while ((in = fopen(fileS[1],"rt")) == NULL)
{
printf("File does not exist on specified directory.\n");
printf("Enter the name of the text file: ");
scanf("%s", fileS);

I've tried to modify the code to allow me to input another file, will this not work? Everytime i try to ask for a new file it just says file does not exist on a specified directory.

Any ideas?

Edit: also just checked the original coding and it seems that it doesn't work in my program at all :(
 
Last edited:
Back
Top Bottom