Access Function header file: unistd. h function: determine the access permissions of files or folders. Check the access method of a file, such as read-only and write-only. If the specified access method is valid, the function returns 0; otherwise, the function returns-1. Usage: intaccess (constchar * filenpath, intmode); or int_acc
Access Function header file: unistd. h function: determine the access permissions of files or folders. Check the access method of a file, such as read-only and write-only. If the specified access method is valid, the function returns 0; otherwise, the function returns-1. Usage: int access (const char * filenpath, int mode); or int _ acc
Access Function
Header file: unistd. h
Function: determine the access permissions of files or folders. Check the access method of a file, such as read-only and write-only. If the specified access method is valid, the function returns 0; otherwise, the function returns-1.
Usage: int access (const char * filenpath, int mode); or int _ access (const char * path, int mode );
Parameter description:
Filenpath
File or folder path. The current directory uses the file or folder name directly.
Note: When this parameter is a file, the access function can use all the values of the mode parameter. When this parameter is a folder, the access function value can determine whether the folder exists. In win nt, all folders have read and write permissions.
Mode
Pattern to be judged
The pre-definition in the header file unistd. h is as follows:
# Define R_ OK 4/* Test for read permission .*/
# Define W_ OK 2/* Test for write permission .*/
# Define X_ OK 1/* Test for execute permission .*/
# Define F_ OK 0/* Test for existence .*/
The specific meaning is as follows:
R_ OK only checks whether the user has the read permission
W_ OK only checks whether the write permission is available
X_ OK
F_ OK: only determines whether the object exists.
Example of the access function program: (refer to the Baidu example to add all four methods)
# Include
# Include
Int file_exists (char * filename)
{
/* Determine whether the file exists */
Return (access (filename, F_ OK) = 0 );
}
Int file_read (char * filename)
{
/* Determine whether the object is readable */
Return (access (filename, R_ OK) = 0 );
}
Int file_write (char * filename)
{
/* Determine whether a file can be written */
Return (access (filename, W_ OK) = 0 );
}
Int file_execute (char * filename)
{
/* Determine whether the file is executable */
Return (access (filename, X_ OK) = 0 );
}
Int main (void)
{
Printf ("Does NOTEXIST. FIL exist: % s \ n ",
File_exists ("NOTEXISTS. FIL ")? "YES": "NO ");
Printf ("Does NOTEXIST. FIL read: % s \ n ",
File_read ("NOTEXISTS. FIL ")? "YES": "NO ");
Printf ("Does NOTEXIST. FIL write: % s \ n ",
File_write ("NOTEXISTS. FIL ")? "YES": "NO ");
Printf ("Does NOTEXIST. FIL execute: % s \ n ",
File_execute ("NOTEXISTS. FIL ")? "YES": "NO ");
Return 0;
}
To perform self-testing, run the chmod command to modify the text attributes and use ls to view the text attributes.