|
setbuffunction
void setbuf ( FILE * stream, char * buffer ); Set stream buffer Sets the buffer to be used for I/O operations with the specified stream, which becomes a fully buffered stream, or alternatively, if the argument for buffer is NULL, it disables buffering for the stream, which becomes an unbuffered stream.This function should be called once the file associated with the stream has already been opened but before any input or output operation has taken place. The buffer parameter must point to an array at least BUFSIZ bytes long (BUFSIZ is a constant defined in <cstdio>). With fully buffered streams, writing operations are not intended to be written directly to the device associated with them; the data is accumulated in the buffer and written to the device as a block when it is filled. This writing of the whole block can be forced by flushing the stream, which can be done by calling fflush or by closing the file (fclose). All buffers are also flushed when the program terminates. With unbuffered streams, data is written to the physical device as soon as possible after each writing operation. All files are opened with a default allocated buffer. This function can be used to define a user-allocated buffer or to disable buffering for a specific stream. System standard streams like stdout and stderr are unbuffered by default unless they are redirected. To change to a line buffered stream, use setvbuf instead. Parameters
Return ValuenoneExample
Two files are opened with writing access. The stream associated with the file example1.txt is set to a user allocated buffer; a writing operation to it is performed; the data is logically part of the stream, but it has not been writen to the device until the fflush function is called. The second buffer in the example, associated with the file example2.txt, is set to unbuffered, so the subsequent output operation is written to the device as soon as possible. The final result, however, is the same with both buffered and unbuffered streams once the files have been closed. See also
|