First, file descriptor
For Linux, all operations on a device or file are performed through a file descriptor. When you open or create a file, the kernel returns a file descriptor (Non-negative integer) to the process. Subsequent operations on the file require only the file descriptor, and the kernel records information about the open file (the file structure body).
When a process starts, 3 files are opened by default, standard input, standard output, standard error, the corresponding file descriptor is 0 (Stdin_fileno), 1 (Stdout_fileno), 2 (Stderr_fileno), and these constants are defined in the Unistd.h header file.
Fileno: Converts a file pointer to a file descriptor
Fdopen: Converts a file descriptor to a file pointer
Second, what is I/O
Input/output is the process of copying data between main memory and external devices
Device-> memory (input operation)
Memory-> device (output operation)
Advanced I/O
The standard I/O libraries provided by ANSI C are called Advanced I/O, and are often referred to as buffered I/O
Low-level I/O
Usually also called I/O without buffering
Third, the opening of the file closed
Open System call 1:
Function prototypes
int open (const char *path, int flags);
Parameters
Path: The name of the file, which can contain (absolute and relative) paths
Flags: File open mode
return value:
Open success, return file descriptor, open failed, return-1
Open System Call 2:
Function prototypes
int open (const char *path, int flags,mode_t mode);
Parameters
Path: The name of the file, which can contain (absolute and relative) paths
Flags: File open mode