What function does Linux use to open a directory?

青灯夜游
Release: 2023-04-14 10:55:30
Original
1786 people have browsed it

Use the opendir() function to open a directory in Linux. The opendir() function can open the specified directory and return a directory stream in the form of "DIR*". This return value must be used to read and search the specified directory; the syntax is "DIR * opendir(const char * name);" .

What function does Linux use to open a directory?

#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

linux opendir() function: open directory function

##1, header file:

#include <sys/types.h>   
#include <dirent.h>
Copy after login

2, define function

DIR * opendir(const char * name);
Copy after login

3, function description:

opendir() is used to open the directory specified by the parameter name, and return DIR * Directory stream in the form, similar to open(), this return value will be used for subsequent reading and searching of the directory.

4, return value:

If successful, a directory stream of type DIR* will be returned. If the opening fails, NULL will be returned.

5, error code:

  • EACCESS Insufficient permissions .

  • EMFILE The maximum number of files that a process can open simultaneously has been reached.

  • ENFILE The maximum number of files that the system can open simultaneously has been reached.

  • ENOTDIR parameter name is not a real directory.

  • ENOENT The directory specified by parameter name does not exist, or the parameter name is an empty string.

  • ENOMEM Out of core memory.

6, Example:

#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
int main()
{
        int a = opendir("/etc/passwd");
        printf("a = %d\n",a);
        return 0;
}
Copy after login

What function does Linux use to open a directory?

Related recommendations: "

Linux Video Tutorial

The above is the detailed content of What function does Linux use to open a directory?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!