C language Opendir () function: Open Directory function
header file:
#include <sys/types.h> #include <dirent.h>
To define a function:
DIR * OPENDIR (const char * name);
Function Description: Opendir () is used to open the directory specified by the parameter name and returns the directory stream of the dir* form, similar to open (), which is then used to read and search for the directory.
Return value: Success returns the directory stream of the dir* type, and returns null if the open fails.
Error code:
1, eaccess insufficient authority.
2. Emfile has reached the maximum number of files that the process can open at the same time.
3, Enfile has reached the system can open at the same time the maximum number of files.
4, Enotdir parameter name is not a real directory.
5, the directory specified by the enoent parameter name does not exist, or the parameter name is an empty string.
6, Enomem core memory is not enough.
C language Closedir () function: Close directory
header file:
#include <sys/types.h> #include <dirent.h>
To define a function:
Function Description: Closedir () closes the directory stream that the parameter dir refers to.
Return value: Close Success returns 0, failure returns 1, and the reason for the error is in errno.
Error code: EBADF parameter dir is an invalid directory stream.
Example: Reference Readir ().
C language Readdir () function: Reading directory functions
header file:
#include <sys/types.h> #include <dirent.h>
To define a function:
struct dirent * Readdir (dir * dir);
Function Description: Readdir () returns the entry point of the next directory stream for the parameter dir directory. The structure dirent is defined as follows:
struct dirent
{
ino_t D_ino;//d_ino the inode of this directory entry point ff_t
d_off;//d_off the displacement of the directory file at the beginning of this directory into the point
signed short int D_reclen; The length of the D_reclen _name, which does not contain null characters
unsigned char d_type;//d_type d_name refers to the file type d_name filename har
d_name[256];
};
Return value: Success returns to the next directory entry point. Returns NULL if an error occurs or reads to the end of the directory file.
Additional Note: The EBADF parameter dir is an invalid directory stream.
Example
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
main ()
{
DIR * DIR;
struct dirent * PTR;
int i;
dir = Opendir ("/etc/rc.d");
while (ptr = Readdir (dir))!= NULL)
{
printf ("D_name:%s\n", ptr->d_name);
}
Closedir (dir);
Perform:
D_name:.
D_name:..
D_NAME:INIT.D
d_name:rc0.d
d_name:rc1.d
d_name:rc2.d
d_name:rc3.d
d_name:rc4.d
D _NAME:RC5.D
d_name:rc6.d
d_name:rc
d_name:rc.local
d_name:rc.sysinit