Search code examples
cstreambuffer

In which condition the buffer get full


I am reading about stream and buffer. I got that if stream is line buffered then the accumulated characters of buffer get transfered in the from of block whenever the newline character encounter. and if stream is unbuffered then character are intended to appear from the source or appear at the destination as soon as possible without getting store in buffer.

but if stream is fully buffered then the accumulated characters of buffer get transfered in the form of block whenever the buffer get totally filled. Now I am unable to understand that in which condition the buffer get totally filled.


Solution

  • The C standard does not explicitly specify the details of stream buffering, but generally a stream has a buffer of fixed size. This is simply an array of bytes that is used for holding data for the stream.

    Quite simply, the buffer is totally filled when the number of bytes that have been written to the stream since the last time the buffer was flushed equals the number of bytes in the buffer.

    When <stdio.h> is included, it defines a macro BUFSIZ that provides the size of the buffer used by setbuf. You can print it with printf("%d\n", BUFSIZ);. Presumably that is the default size of a buffer for a stream, although the C standard does not explicitly say this. (It says that is the size of the buffer used by the setbuf function, which allows you to provide your own memory for the buffer.)

    You can also use the newer setvbuf to request a different size for the buffer. Both setbuf and setvbuf must be used only just after a stream has been opened and before any other operation.