Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
C Library
cassert (assert.h)
cctype (ctype.h)
cerrno (errno.h)
cfloat (float.h)
ciso646 (iso646.h)
climits (limits.h)
clocale (locale.h)
cmath (math.h)
csetjmp (setjmp.h)
csignal (signal.h)
cstdarg (stdarg.h)
cstddef (stddef.h)
cstdio (stdio.h)
cstdlib (stdlib.h)
cstring (string.h)
ctime (time.h)
cstdio (stdio.h)
functions:
clearerr
fclose
feof
ferror
fflush
fgetc
fgetpos
fgets
fopen
fprintf
fputc
fputs
fread
freopen
fscanf
fseek
fsetpos
ftell
fwrite
getc
getchar
gets
perror
printf
putc
putchar
puts
remove
rename
rewind
scanf
setbuf
setvbuf
sprintf
sscanf
tmpfile
tmpnam
ungetc
vfprintf
vprintf
vsprintf
macro constants:
EOF
FILENAME_MAX
NULL
TMP_MAX
objects:
stderr
stdin
stdout
types:
FILE
fpos_t
size_t


setbuf

function
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

stream
Pointer to a FILE object that identifies an open stream.
buffer
User allocated buffer. Must have a length of at least BUFSIZ bytes, which is a macro constant defined in <cstdio> specifically designed to be used as the length of this array.
Alternatively, a NULL pointer can be specified to disable buffering.

Return Value

none

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/* setbuf example */
#include <stdio.h>
int main ()
{
  char buffer[BUFSIZ];
  FILE *pFile1, *pFile2;
  pFile1=fopen ("myfile.txt","w");
  pFile2=fopen ("myfile2.txt","a");
  setbuf ( pFile1 , buffer );
  fputs ("This is sent to a buffered stream",pFile1);
  fflush (pFile1);
  setbuf ( pFile2 , NULL );
  fputs ("This is sent to an unbuffered stream",pFile2);
  fclose (pFile1);
  fclose (pFile2);
  return 0;
}


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