Format
#include <stdio.h> void setbuf(FILE *stream, char *buffer);
Language Level: ANSI, POSIX, XPG4
setbuf controls buffering for the specified stream.
If the buffer argument is NULL, the stream is unbuffered. If not, the buffer must point to a character array of length BUFSIZ, which is the buffer size defined in the <stdio.h> include file. The system uses the buffer, which you specify, for input/output buffering instead of the default system-allocated buffer for the given stream.
setbuf is similar to setvbuf, however setvbuf is more flexible and may supercede setbuf in the future.
Note: Streams are fully buffered by default, with the exceptions of stderr, which is line-buffered, and memory files, which are unbuffered.
Return Value
There is no return value.
Example
This example opens the file setbuf.dat for writing. It
then calls setbuf to establish a buffer of length BUFSIZ. When
string is written to the stream, the buffer buf is used and
contains the string before it is flushed to the file.
#include <stdio.h> #include <string.h>
int main(void)
{
char buf[BUFSIZ];
char string[] = "hello world";
FILE *stream;
memset(buf,'\0',BUFSIZ); /* initialize buf to null characters */
stream = fopen("FILENAME", "wb");
setbuf(stream, buf); /* set up buffer */
fwrite(string, sizeof(string), 1, stream);
printf("%s\n", buf); /* string is found in buf now */
fclose(stream); /* buffer is flushed out to output stream */ return 0;
/*********************************************************************
The output file should contain:
hello world
The output should be:
hello world *********************************************************************/ }
![]()
fclose -- Close Stream
fflush -- Write Buffer to File
![]()
_flushall -- Write Buffers to Files
fopen -- Open Files
setvbuf -- Control Buffering
<stdio.h>