setbuf

Description

Controls stream buffering.

#include <stdio.h>

void setbuf( FILE *stream, char *buffer);

stream Pointer to FILE structure  
buffer User-allocated buffer  

Remarks

The setbuf function allows the user to control buffering for stream. The stream argument must refer to an open file that has not been read or written. If the buffer argument is NULL, the stream is unbuffered. If not, the buffer must point to a character array of length BUFSIZ, where BUFSIZ is the buffer size as defined in STDIO.H. The user-specified buffer, instead of the default system-allocated buffer for the given stream, is used for I/O buffering.

The stderr and (in DOS only) stdaux streams are unbuffered by default, but can be assigned buffers with setbuf.

The setbuf function has been subsumed by the setvbuf function, which should be the preferred routine for new code. The setbuf function is retained for compatibility with existing code.

Return Value

None.

Compatibility

Standards:ANSI, UNIX

16-Bit:DOS, QWIN, WIN, WIN DLL

32-Bit:DOS32X

See Also

fclose, fflush, fopen, setvbuf

Example

/* SETBUF.C: This program first opens files named DATA1 and DATA2.

* Then it uses setbuf to give DATA1 a user-assigned buffer

* and to change DATA2 so that it has no buffer.

*/

#include <stdio.h>

void main( void )

{

char buf[BUFSIZ];

FILE *stream1, *stream2;

if( ((stream1 = fopen( "data1", "a" )) != NULL) &&

((stream2 = fopen( "data2", "w" )) != NULL) )

{

/* "stream1" uses user-assigned buffer: */

setbuf( stream1, buf );

printf( "stream1 set to user-defined buffer at: %Fp\n", buf );

/* "stream2" is unbuffered */

setbuf( stream2, NULL );

printf( "stream2 buffering disabled\n" );

_fcloseall();

}

}

Output

stream1 set to user-defined buffer at: 0298:0DF2

stream2 buffering disabled