0

Possible Duplicate:
C check if file exists

I was just wondering if someone could point me in the right direction regarding to how I should go about checking if a file exists? At this point in time I plan on going with something along the lines of the following;

if(NULL==(fp=fopen(filepath, "r"))){

my ultimate goal would be to find a solution to replace this in bash (just the file finding part)

if ! (find /dev/shm/request-summon-*-$MCEXEC_PLAYERNAME.txt > /dev/null 2>&1); then

any help is really appreciated, thanks!

Community
  • 1
  • 1
lacrosse1991
  • 2,972
  • 7
  • 38
  • 47
  • The bash command seems rather misplaced. You can play around with `shopt -s nullglob` and see if the wildcard's expansion is empty. See e.g. http://mywiki.wooledge.org/NullGlob – tripleee May 20 '12 at 20:07
  • @tripleee nullglob option is actually used in the bash script already, but I am looking for a way to do this in C instead, bash version works just fine :) – lacrosse1991 May 20 '12 at 20:15

3 Answers3

1

Look at the manual page for stat() also covering lstat() and fstat(). If you get -1 as the return value, the file you tried to get summary information on does not exist, or may not have rights to read it (see last example below which shows the difference from your fopen() test as a file may exist but not let you open it).

edd@max:/tmp$ cat stat.c

#include <stdio.h>

int main(int argc, char *argv[]) {
    if (argc > 1)
        printf("Returned %d for %s\n", stat(argv[1]), argv[1]);
}
edd@max:/tmp$ gcc -o stat stat.c
edd@max:/tmp$ ./stat stat.c
Returned 0 for stat.c
edd@max:/tmp$ ./stat does-not-exist
Returned -1 for does-not-exist
edd@max:/tmp$ ./stat /root/.ssh/id_dsa    ## exists, but I can't read it
Returned -1 for /root/.ssh/id_dsa
edd@max:/tmp$
Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • Do you have a standard solution, too? – Shahbaz May 20 '12 at 20:05
  • Nop. C doesn't know about directories but you can use POSIX opendir/scandir to emulate your `find`. – Benoît May 20 '12 at 20:08
  • I would call this fairly standard. From the manual page I mentioned: "These system calls conform to SVr4, 4.3BSD, POSIX.1-2001." (and continues to detail where results in the `stat` structure may differ). For 'existence tests' this is pretty standard. – Dirk Eddelbuettel May 20 '12 at 20:13
0

Use stat() or lstat(). Personally I prefer using lstat() because it tells you whether a directory entry is a symbolic link, rather than following the link.

For your ultimate goal, traverse the directory tree. You can do this with opendir() and readdir().

Walter
  • 803
  • 6
  • 7
0

On a POSIX system, you can try to open the file and check whether errno == ENOENT if it fails (returns NULL):

if ((fp = fopen(path, "r")) == NULL)
    if (errno == ENOENT)
        // file doesn't exist

There's no pure ISO C way of doing this.

Using stat or lstat is also possible, but can lead to a race condition when the file is created in between the stat and the opening of the files.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836