Controls stream buffering and buffer size.
#include <stdio.h>
int setvbuf( FILE *stream, char *buffer, int mode, size_t size );
stream | Pointer to FILE structure | |
buffer | User-allocated buffer | |
mode | Mode of buffering: _IOFBF (full buffering), _IOLBF (line buffering), _IONBF (no buffer) | |
size | Size of buffer |
The setvbuf function allows the program to control both buffering and buffer size for stream. The stream must refer to an open file that has not been read from or written to since it was opened. The array pointed to by buffer is used as the buffer, unless it is NULL, and an automatically allocated buffer size bytes long is used.
The mode must be _IOFBF, _IOLBF, or _IONBF. If mode is _IOFBF or _IOLBF, then size is used as the size of the buffer. If mode is _IONBF, the stream is unbuffered and size and buffer are ignored.
Values for mode and their meanings are:
Type | Meaning |
_IOFBF | Full buffering; that is, buffer is used as the buffer and size is used as the size of the buffer. If buffer is NULL, an automatically allocated buffer size bytes long is used. |
_IOLBF | With DOS, the same as _IOFBF. |
_IONBF | No buffer is used, regardless of buffer or size. |
The legal values for size are greater than 0 and less than 32,768.
The return value for setvbuf is 0 if successful, and a nonzero value if an illegal type or buffer size is specified.
Standards:ANSI, UNIX
16-Bit:DOS, QWIN, WIN, WIN DLL
32-Bit:DOS32X
/* SETVBUF.C: This program opens two streams named stream1 and stream2.
* It then uses setvbuf to give stream1 a user-defined buffer of 1024
* bytes and stream2 no buffer.
*/
#include <stdio.h>
void main( void )
{
char buf[1024];
FILE *stream1, *stream2;
if( ((stream1 = fopen( "data1", "a" )) != NULL) &&
((stream2 = fopen( "data2", "w" )) != NULL) )
{
if( setvbuf( stream1, buf, _IOFBF, sizeof( buf ) ) != 0 )
printf( "Incorrect type or size of buffer for stream1\n" );
else
printf( "'stream1' now has a buffer of 1024 bytes\n" );
if( setvbuf( stream2, NULL, _IONBF, 0 ) != 0 )
printf( "Incorrect type or size of buffer for stream2\n" );
else
printf( "'stream2' now has no buffer\n" );
_fcloseall();
}
}
'stream1' now has a buffer of 1024 bytes
'stream2' now has no buffer