Detailed introduction to file descriptors and file pointers under Linux

黄舟
Release: 2017-06-02 09:56:11
Original
1829 people have browsed it

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

index

of 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.

IntegerValueConstant0Standard inputFILE ##1Standard outputSTDOUT_FILENOstdout2Standard errorSTDERR_FILENOstderr opens an existing file or creates a new file, the kernel returns a file descriptor to the process. In programming, some low-level programming often revolves around file descriptors. However, the concept of file descriptors is often only applicable to operating systems such as UNIX and Linux.
Name(unistd.h)Symbol (stdio.h)File stream
STDIN_NO stdin
The file descriptor is at Formally it is a non-negative integer. In fact, it is an index value pointing to the record table of files opened by the process maintained by the kernel for each process. When a program

Operations related to file descriptors:


Function

Operation

Operations related to a single file descriptorendchmod(), etc. With file descriptors Table correlation operationsFile pointer:The file pointer is what we learn in C language
File descriptor generation open(), socket(), pipe(), etc.
read(), write(), s(), f
Operations related to plural file descriptorsselect(), poll(), etc.
close(), dup(), etc.
. In C language, a pointer

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:

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

object

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);
Copy after login

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!

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!