C- File I / O Buffers and setvbuf ()

In C, does fopen () actually create two buffers, one for input and one for output?

Here is what my C book says:

Usually the first step when using standard I / O is to use the f open () function to open the file. (Recall, however, that stdin, stdout, and stderr files open automatically.) The fopen () function not only opens the file, but sets up a buffer (two buffers for read and write modes) , and it sets up a data structure containing data about the file and. ..

If opening a file using the fopen () function creates two buffers in write mode, for example, "a +", i.e. read and write

FILE * fp = fopen ("file.txt", "a +"); setvbuf (destination_file, NULL, _IOFBF, BUFFER_SIZE); 

what buffer does setvbuf () do?

+5
source share
2 answers

An open file has only one buffer, whether it was open for reading, writing, or both.

Section 7.21.5.3 of the C standard , which details the function fopen , states:

7 When a file is opened in update mode ( + as the second or third character in the above list of values โ€‹โ€‹of the mode argument), both inputs and outputs can be performed in the corresponding stream. However, the output should not directly follow the input without an intermediate fflush function or file positioning function ( fseek , fsetpo s, or rewind ), and the input should not be directly followed by output without an intermediate call to the file positioning function if the input operation does not meet the end of the file. Opening (or creating) a text file with update mode may instead open (or create) a binary stream in some implementations.

The paragraph above says that the output buffer must be flushed before performing the input (explicitly or implicitly using the positioning function), as well as when performing the output after input. This is a consequence of having only one buffer.

This also makes sense from a logical point of view, as it prevents reading and writing from reading due to an inconsistent presentation of the contents of the file.

+4
source

what buffer does setvbuf () do?

"Both" of them. There is no requirement that the fopen() call create "two buffers for read-write modes." Most implementations use a single buffer that everyone needs.

The C standard implicitly supports a single buffer. Per 7.21.5.3 fopen Function , Clause 7 :

When a file is opened in update mode ( '+' as the second or third character in the above list of values โ€‹โ€‹of the mode argument), both input and output can be performed on the stream. However, the output should not be an input without an intermediate call to the fflush function or to the file positioning function ( fseek , fsetpos or rewind ), and the input should not be accompanied by a direct exit to an intermediate call to the file positioning function, if only the input operation meets the end of the file. Opening (or creating) a text file with update mode may instead open (or create) a binary stream in some implementations.

The requirements of this paragraph allow the use of a single buffer.

+3
source

Source: https://habr.com/ru/post/1275215/


All Articles