The standard I/O Library provides buffer to minimize the number of Read and Write calls. It also automatically performs buffer management for each I/O Stream, thus avoiding the trouble that the application needs to consider this. Unfortunately, the most confusing thing about the standard I/O Library is its buffer. Standard I/O provides three types of buffering: 1. Full buffer. In this case, the actual I/O operation is performed only after the standard I/O buffer is filled. Files that reside on the disk are usually fully buffered by the standard I/O library. When a stream performs the first I/O operation, the related standard I/O functions usually call malloc to obtain the buffer to be used. The write operation on the I/O buffer zone is described in terms of flushing. The buffer can be automatically rinsed by the standard I/O routine, or the fflush function can be called to fl a stream. It is worth noting that flush has two meanings in the Unix environment. In terms of the standard I/O library, flush thinks that this writes the content in the buffer to the disk. Flush indicates that data already stored in the buffer zone is discarded in the terminal driver. 2. Row buffering. In this case, when a line break is encountered in the input and output, the standard I/O Library performs the I/O operation. This allows us to output one character at a time, but the actual I/O operation is performed only after a line is written. When a stream involves a terminal, it usually uses row buffering. There are two restrictions on Row buffering. First, because the buffer length used by the standard I/O library to collect each row is fixed, as long as the buffer is filled up, I/O operations are performed even if no line break is written. Second, any time as long as the standard I/O Library requests the input data from the stream buffered by a bag or the stream buffered by B rows, then, the output stream is buffered for all rows. The reason that a description in parentheses is included in B is that the required data may already be in the buffer zone and does not need to be read from the kernel when data is needed. Obviously, data is obtained from the kernel from input requests in a stream without buffering. 3. No buffer. The standard I/O Library string is buffered for storage. For example, if the iaozhun I/O function fputs is used to write 15 characters to a stream without buffering, this function is likely to use the Write System to call the function to immediately write these characters to the associated open file. Standard Error slide stderr is usually not buffered, so that the error information can be displayed as soon as possible, regardless of whether they contain a line break. Iso c requires the following buffer features: When and only when the standard input and standard output do not involve interactive devices, they are fully buffered. The standard will not enable full buffering. However, this does not tell us if the standard input and standard output involve interactive devices, whether they are buffered or line-buffered; and the row buffer is not included in the standard error. By default, the system uses the following types of buffering: Standard errors are not buffered. If it involves other streams of terminal devices, they are buffered; otherwise, they are fully buffered. For any given stream, if we do not like the default situation of these systems, we can call one of the following functions to change the buffer type: Void setbuf (File * restrict FP, char * restrict BUF) Int setvbuf (File * restrict FP, char * restrict Buf, int mode, size_t size)
Let's look at a small example. Source program: |