The access( ) function checks for the existence of a file and also determines whether it can be read,written to or executed. This function takes two arguments the filename and an integer indicating the access mode. The values 6, 4, 2, and 1 checks for read/write, read, write and execute permission of a given file, whereas value 0 checks whether the file exists or not. Following program demonstrates how we can use access( ) function to check if a given file exists.
char fname ;
printf ( "\nEnter name of file to open" ) ;
gets ( fname ) ;
if ( access ( fname, 0 ) != 0 )
printf ( "\nFile does not exist." ) ;