-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfexists.c
More file actions
50 lines (28 loc) · 976 Bytes
/
Copy pathfexists.c
File metadata and controls
50 lines (28 loc) · 976 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/* fexists.c */
/* Look for a file in the current directory. */
/* Return 0 if found, and -1 if not found. */
/* This code is released to the public domain. */
/* "Share and enjoy....." ;) */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* USAGE: */
/* On the command line ( $ is command prompt) - */
/* $ ./fexists foo.c */
/* OR - */
/* $ ./fexists "foo.c" */
/* Either method should work. They do for me. */
int fexists(char *fname)
{
FILE *fptr ;
fptr = fopen(fname, "r") ;
if ( !fptr ) return -1 ; /* File does not exist in dir. */
fclose(fptr);
return 0; /* File DOES exist in dir. */
}
int main( int argc, char *argv[])
{
/* See if a file exists. */
printf("Result is %d \n", fexists(argv[1]) ) ;
return 0;
}