Acess () Determine if the directory exists

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.