Use Function access. The header file is io. h. prototype: int access (const char * filename, int amode );
If the amode parameter is 0, the object existence is checked. If the object exists, 0 is returned. If the object does not exist,-1 is returned.
This function can also check other file attributes:
06 check read/write permissions 04 check read permission 02 check write permission 01 check execution permission 00 check file existence under UNIX and VC the experiment was successful. The advantage is that fopen (..., "r") is not good. When there is no read permission, it will not work. Even if the file does not have the read permission, you can determine whether the file exists and returns 0. If the file does not exist,-1 # include <stdio is returned. h> int main () {printf ("% d", access ("111", 0 ));
Bytes --------------------------------------------------------------------------------------------
# Include <io. h> # include <stdio. h> # include <stdlib. h>
Void main (void) {/* Check for existence */if (_ access ("ACCESS. C", 0 ))! =-1) {printf ("File ACCESS. C exists \ n ");/* Check for write permission */if (_ access (" ACCESS. C ", 2 ))! =-1) printf ("File ACCESS. C has write permission \ n ");}}
Output
File ACCESS. C exists File ACCESS. C has write permission