C Language Method method for judging whether a folder or file exists: Access function determines whether a folder or file exists
Function prototypes: int access (const char *filename, int mode);
Owning header file: io.h
FileName: can fill in folder path or file path
mode:0 (F_OK) only determines if there is
2 (R_OK) to determine write permissions
4 (W_OK) to determine Read permissions
6 (X_OK) Judge execution permissions
To determine if a folder exists, mode takes 0 and determines if a file exists, mode can take 0, 2, 4, 6. The return value is 0 if it exists or has permission, no or no permission, and the return value is-1.
Error code
The file specified by the eaccess parameter pathname does not meet the permissions required for the test.
Erofs files to test write permissions exist in the read-only file system.
The Efault parameter pathname pointer is out of the accessible memory space.
The EINVAL parameter mode is incorrect.
Enametoolong parameter pathname too long.
The Enotdir parameter pathname is a directory.
Enomem Core memory is low
The Eloop parameter pathname has too many symbolic connection problems.
EIO I/O access error.
Special CAUTION: Use Access () for user authentication to be particularly cautious, such as an empty file after access () to open () may cause problems with the system security.
Instance:
#include <stdio.h>
#include <io.h>
int main (void)
{
if (!access ("C://windows", 0))
Puts ("C://windows exisits!");
Else
Puts ("C://windows doesn ' T exisit!");
return 0;
}
Method Two: fopen function to determine whether a file exists
Function prototypes: FILE *fopen (char *filename, char *type);
FileName: File path
Type: How to open a file (R, W, r+, w+, A, RB, WB, etc.)
Used to determine if a file exists, R or RB can be used, because the file may be created automatically if other means are used. The return value is null (cannot be opened) and a positive number (can be opened).
A special reminder: the judgment in this way is not entirely correct, because some files exist, but may not be readable.
C language to determine whether a folder or file exists method "Go"