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 <errno. h> # include <unistd. h> # include <stdio. h> 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.