Let's take a look at how the C standard I/O library functions are implemented using system calls.
fopen (3)
Calling open (2) Opens the specified file, returns a file descriptor (that is, an int type number), assigns the file structure, which contains information such as descriptor, I/O buffer, and current read/write location, and returns the address of this file structure.
FGETC (3)
A descriptor for the file is found by passing in the filename parameter, I/O buffers and current read and write locations, to determine whether the next character can be read from the I/O buffer, or to return the character directly if read, otherwise call read (2), and the file description Fu in to allow the kernel to read the file's data to the I/O buffer , and then returns the next character. Note that for the C standard I/O library, the open file is identified by the file * pointer, and for the kernel, the open file is identified by the document descriptor, the file descriptor is obtained from the open system call, and the file descriptor is required when using the read, write, close system calls.
FPUTC (3)
Determine if there is room for the I/O buffer for the file to hold one character, and if there is room, save it directly in the I/O buffer and return, if the I/O buffer is full, call write (2) so that the kernel writes the contents of the I/O buffer back to the file.
Fclose (3)
If there is still data in the I/O buffer that has not been written back to the file, call write (2) writeback, and then close (2) closes the file, releasing the files structure and I/O buffers.
In the case of write files, the relationship between C standard I/O library functions (printf (3), Putchar (3), fputs (3)) and system call write (2) is shown in the following illustration.
Hierarchical relationship between library functions and system calls