Home >Operation and Maintenance >Linux Operation and Maintenance >Detailed introduction to file descriptors and file pointers under Linux
This article mainly introduces the detailed information about file descriptors and file pointers under Linux. Friends in need can refer to
File descriptors under Linux Descriptors and file pointers
File identifier:
First of all, let’s understand what a file identifier is?
File description A symbol is a term in computer science and is an abstract concept used to express a reference to a file. In Linux, each process will save a file descriptor table in the process control block (PCB). The file descriptor is the
indexof this table. Each table has There is a pointer to the open file. Each Linux process should have three standard file descriptors, corresponding to three standard streams.
Operations related to file descriptors:
variablepoints to a file. This pointer is called a file pointer. Through the file pointer, we can perform various operations on the pointed file. The C language uses file pointers instead of file descriptors as I/O handles. The file pointer points to a data structure called the FILE structure in the user area of the process. The FILE structure contains a buffer and a file descriptor.
The file pointer is a common format of C language, so it is relatively easier to transplant.
We usually obtain the file pointer: FILE *fp = fopen("test.txt","r");
After obtaining the pointer, We can get the information of the file descriptor in the pointer. The file descriptor is unique, but the file pointer is not unique. The
pointed to by the file pointer is unique.
Conversion of file descriptors and file pointers:int fileno(FILE *stream); FILE *fdopen(int fd,const char *mode);
The first parameter of fdopen is an open file descriptor, and mode indicates the opening methodString, the same as fopen, such as "w" or "r".
filenoFunction, it returns the file descriptor corresponding to the file stream, and returns - on failure 1.
The above is the detailed content of Detailed introduction to file descriptors and file pointers under Linux. For more information, please follow other related articles on the PHP Chinese website!