The way I'm using just involves trying to fopen() the file to be checked,
/* --- does file exist??? --- */ char fname[999] = "whatever"; /* constructed during execution */ FILE *fp = NULL; /* try to fopen(fname,"r") */ int isfilefound = 0; /* set true if fopen() succeeds */ if ( (fp = fopen(fname,"r")) /* try to fopen() for read */ != NULL ) { /* succeeded */ isfilefound = 1; /* set file found flag */ fclose(fp); } /* and just close the file */ Is there a quicker, less resource-intensive, way?... A specific way for unix/linux? A Windows way? And preferably, a portable posix-compliant way (as above presumably is)? It's being done lots (1000's) of times, so I'd prefer not to be unnecessarily opening and closing files for no good reason.
-----------------------------------------------------------------
Edit Okay, based on answers below, I put together the following little function intended to check whether or not file (already:) exists in a posix,windows,other portable way...
/* ========================================================================== * Function: isfilexists ( path ) * Purpose: check whether file at path exists * -------------------------------------------------------------------------- * Arguments: path (I) pointer to null-terminated char string * containing "path/filename.ext" of * file whose existence is to be determined * (path is relative to pwd unless explicitly * absolute by initial '/' or other syntax) * -------------------------------------------------------------------------- * Returns: ( int ) 1 if file at path exists, or 0 if not * -------------------------------------------------------------------------- * Notes: o conditional compiles for various systems, * depending on whether POSIX or WINDOWS is #define'ed... * o ...method used: * 1: use access() on Posix systems, * 2: PathFileExists() on Windows systems, * 3: fopen() on any other systems. * ======================================================================= */ /* --- entry point --- */ int isfilexists ( char *path ) { /* --- * allocations and declarations * ------------------------------- */ int isexists = 0; /* set true if file at path exists */ FILE *fp = NULL; /* fopen() for non-posix,windows */ #define POSIX /* just for testing */ /* --- * determine whether file at path already exists * ------------------------------------------------ */ #if defined(POSIX) /* posix-compliant system... */ #include <unistd.h> if ( access(path,F_OK) == 0 ) /* file at path exists */ isexists = 1; /* so set file exists flag */ #else #if defined(WINDOWS) /* Windows system... */ isexists = PathFileExists(path); /* set flag if file at path exists */ #else /* --- fopen() for any other non-posix, non-windows system --- */ if ( (fp = fopen(path,"r")) /* try to fopen() for read */ != NULL ) { /* succeeded */ isexists = 1; /* set file exists flag */ fclose(fp); } /* and just close the file */ #endif #endif return ( isexists ); /* back to caller with 1 if file at path exists */ } /* --- end-of-function isfilexists() --- */ The access() and fopen() methods tested and work okay. Unable to test PathFileExists() for windows. And I still want to figure out what #define'ed symbols to automatically and unambiguously check for conditional compiles.
access()orstat(). But the question is: why do you want to do this check? Checking for existence of the file is quite often the wrong approach.