In the program, for the file read and write, the program run time is mainly consumed on the I/O. Compared to read/write memory, the files on the read-write drive are much slower, and the program run time is mostly consumed on I/O when there is little content to read/write to the file. Therefore, buffered I/O and non-buffered I/O are available.
Non-buffered I/O, each time for the operation of the file, I/O operations. For example Linux system strips are used:
Open, read, write, Lseek, close.
They rely on the operating system, do not have a file structure pointer (the file as a binary file), directly manipulate the file, these operations do not pass through the buffer. For example, write a file, write the content directly to the physical disk. The advantage is the ability to quickly write content to a file, in the event of an emergency (such as a power outage), the content will not be lost. The disadvantage is the frequent I/O operation, which consumes CPU.
fopen, Fclose, Fread, Fwrite, fgetc, Fgets, FPUTC, fputs, Freopen, fseek, Ftell, rewind
buffer I/O, is to open a buffer in memory, the operation of the file has a buffer, the "need" when the write to disk. In general, the larger the buffer, the less actual I/O is, and the more efficient it is.
They depend on the file struct pointer, which can read characters, strings, and formatted data.
The function of standard I/O is buffered I/O operations:
Buffered I/O has full buffering and row buffering:
Full buffering is the actual I/O operation after the buffer fills up. Files on disk are often fully buffered by standard I/O library functions.
A row buffer is the actual I/O operation when a newline character is encountered in the input or output.
difference:
The Fopen series functions are provided by the standard library, using the file structure pointer to process the file as a stream.
The Open Series function is a system bar, using a file descriptor.
Everything in Linux is file, but fopen only refers to ordinary files. Device files cannot be processed as streaming files, only the open series functions can be used.
Fopen enables the process to have a buffer in the user state, unlike the open series functions, each time the system bar is used, the user state/kernel state switch.