Acess () function Description:
Checks whether the calling process can perform some action on the specified file.
<pre lang= "C" escaped= "true" >
#include <unistd.h>
int access (const char *pathname, int mode);
</pre>
Parameter description:
Pathname: The path name of the file that needs to be tested.
Mode: The operating modes that need to be tested, the possible values are one or <strong> more </strong>.
<ol>
<LI>R_OK (readable?),</li>
<li> W_OK (writable), </li>
<LI>X_OK (executable?) </li>
<li> or F_OK (file exists?) Assembly. </li>
</ol>
<blockquote> in fact, the most used is to use F_OK to check whether the directory exists. </blockquote>
Return Description:
When executed successfully, returns 0. The failed return -1,errno is set to one of the following values
<ol>
<li>einval: Invalid mode value </li>
<li>eacces: The directory contained in the file or path name is not accessible </li>
<li>eloop: There are too many symbolic connections in the process of interpreting pathname </li>
<li>enametoolong: path name too long </li>
<li>enoent: The directory in the pathname does not exist or is not valid for symbolic connections </li>
<li>enotdir: The component in the path name as a directory is not a directory </li>
<li>erofs: File system Read Only </li>
<li>efault: path name points to accessible outer Space </li>
<li>eio: Input/Output error </li>
<li>enomem: Unable to get enough kernel memory </li>
<li>etxtbsy: Write error to program </li>
</ol>
<pre lang= "C" escaped= "true" line= "1" >
int main (int argc, char *argv[])
{
if (ARGC < 2) {
printf ("Usage:./test filename\n");
Exit (1);
}
if (Access (argv[1], f_ok) = =-1) {
Puts ("File not exists!");
Exit (2);
}
if (Access (argv[1], r_ok) = =-1)
Puts ("You can ' t read the file!");
Else
if (Access (argv[1), R_OK | W_OK)! =-1)
Puts ("You can read and write the file");
Else
Puts ("You can read the file");
Exit (0);
}
</pre>
Acess () Determine if the directory exists