Syntax
#include <stdio.h> void setbuf(FILE *stream, char *buffer);Description
setbuf controls buffering for the specified stream. The stream pointer must refer to an open file before any I/O or repositioning has been done.
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.
setvbuf is more flexible than setbuf.
Note: Streams are fully buffered by default, with the exceptions of stderr, which is line-buffered, and memory files, which are unbuffered.
There is no return value.
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>
#define FILENAME "setbuf.dat"
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
****************************************************************************/
}
Related Information