Directory operation function of "Linux C Chinese function manual"

Source: Internet
Author: User
Tags define function

Directory operation function 1) closedir close directory

Correlation function: Opendir
Table header files: #include <sys/types.h>
#include <dirent.h>
define function: int closedir (DIR *dir);
Function Description: Closedir () closes the directory stream that the parameter dir refers to.
Return value: Close succeeds returns 0, failure returns-1, and the reason for the error is stored in errno.
Error code: EBADF parameter dir is not an invalid directory stream


2) Fchdir Change the current working directory

Correlation function: Getcwd,chroot
Table header files: #include <unistd.h>
define function: int fchdir (int fd);
Function Description: Fchdir () is used to change the current working directory to the file descriptor referred to by the parameter FD.
Return value: The execution succeeds returns 0, and the failure returns -1,errno as the error code.
Example:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
void Main ()
{
int FD;
FD = open ("/tmp", o_rdonly);
Fchdir (FD);
printf ("Current working directory:%s \ n", GETCWD (Null,null));
Close (FD);
}
Execution Result:
Current working directory:/tmp

3) Opendir Open Directory

Correlation function: Open,readdir,closedir,rewinddir,seekdir,telldir,scandir
Table header files: #include <sys/types.h>
#include <dirent.h>
Define 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* pattern, similar to open (),
This return value is then used to read and search the directory.
Return value: Success returns a directory stream of type dir*, and null if open fails.
Error code:
eaccess Insufficient Permissions
Emfile has reached the maximum number of files that the process can open simultaneously.
Enfile has reached the maximum number of files that the system can open simultaneously.
Enotdir parameter name is not a real directory
The ENOENT parameter name specifies a directory that does not exist, or the parameter name is an empty string.
Enomem core memory is low.

4) readdir Read directory

Correlation function: Open,opendir,closedir,rewinddir,seekdir,telldir,scandir
Table header files: #include <sys/types.h>
#include <dirent.h>
Define function: struct dirent * Readdir (dir * dir);
Function Description: Readdir () returns the next directory entry point for the Dir directory stream.
The structure dirent is defined as follows
struct dirent
{
Long D_ino; /* Inode number Index Node # */
off_t D_off; /* Offset to this dirent in the directory file */
unsigned short d_reclen; /* Length of this d_name filename */
unsigned char d_type; /* The type of d_name file type */
Char d_name [name_max+1]; /* File name (null-terminated) file name, maximum 256 characters */
}
Return value: Success returns to the next directory entry point. An error occurred or was read to the end of the directory file to return NULL.
Additional description: The EBADF parameter dir is an invalid directory stream.

5) Rewinddir Reset Read directory to the starting position

Correlation function: Open,opendir,closedir,telldir,seekdir,readdir,scandir
Table header files: #include <sys/types.h>
#include <dirent.h>
Define function: void Rewinddir (DIR *dir);
Function Description: Rewinddir () is used to set the parameter dir stream where the current read position is at the beginning of the read position.
Error code: EBADF dir is not an invalid directory stream

6) Seekdir set the next read directory location

Correlation function: Open,opendir,closedir,rewinddir,telldir,readdir,scandir
Table header files: #include <dirent.h>
Define function: void Seekdir (DIR * dir,off_t offset);
Function Description: Seekdir () is used to set the current read position of the Dir directory stream, and to start reading from this new location when Readdir () is called.
The parameter offset represents the offset from the beginning of the catalog file.
Error code: EBADF parameter dir is not an invalid directory stream

7) Telldir Get the read location of the directory stream

Correlation function: Open,opendir,closedir,rewinddir,seekdir,readdir,scandir
Table header files: #include <dirent.h>
Define function: off_t telldir (DIR *dir);
Function Description: Telldir () returns the current read position of the Dir directory stream. This return value represents the distance
The offset return value at the beginning of the catalog file returns the next read location, and returns 1 when an error occurs.
Error code: EBADF parameter dir is an invalid directory stream.

Directory operation function of "Linux C Chinese function manual"

Related Article

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.