Linuxaccess () function and readdir () function 1 linuxaccess () function and readdir () function 1.1 access () access function prototype: intaccess (constchar * pathname, intmode ); this function is used to obtain the pathname pointed to by the calling process...
Linux access () function and readdir () function 1 the prototype of the linux access () function and the readdir () function 1.1 access () access function is as follows: int access (const char * pathname, int mode); this function is used to obtain the access permission of the calling process to the file (regular) or directory (directory) pointed to by pathname. Pathname: file or directory path mode: access mode. You can use F_ OK, or F_ OK, R_ OK, W_ OK, and X_ OK or (| ). F_ OK indicates whether the file indicated by pathname exists. Write a program to check whether the directory exists: www.2cto.com # include # Include # Include Int main () {int err = 0; err = access ("/home/test", F_ OK); if (err! = 0) {printf ("erro! % D: % s \ n ", errno, strerror (errno); www.2cto.com mkdir ("/home/test ", 0777); printf (" erro! % D: % s \ n ", errno, strerror (errno);} else printf (" direxit! \ N "); return 0;} create a test file under the/home directory and test the above program. 1. if the input pathname is "/home/test" and the last string of the path is not the slash '/', the result is as follows: root @ VM-Ubuntu203001 :~ /Test #./a. out dir exit! Www.2cto.com because test is a file under/home, access is correct to determine that the file already exists. 2. if the input pathname is "/home/test/", the last string of the path is the slash '/', the result is as follows: root @ VM-Ubuntu203001 :~ /Test #./a. out erro! 20: Nota directory erro! 17: The File exists test result is that access treats the test File under the/home directory as a directory, and finally finds that test is not a directory, so an error is reported. After searching the document, we know that if the last character in the linux file path is the slash '/', linux treats this path as a directory path, regardless of whether the directory name in the path is actually an existing file name. 1.2 readdir () function prototype: www.2cto.com DIR * opendir (const char * dirpath); struct dirent * readdir (DIR * dirp); opendir () function opens a directory pointed to by dirpath, if a DIR pointer is returned, the DIR pointer points to the first entry in the dirpath directory. Readdir () returns the information of this entry, which is displayed in the form of struct dirent, and the DIR pointer points to the next entry. Note that the entries traversed by DIR are not sorted, which is consistent with the order listed by ls-f.
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