Borland C++ reference

setbuf

raw OCR

Assigns buffering to a stream.

Defined in header <stdio.h>

Syntax

#include <stdio.h>
void setbuf(FILE *stream, char *buf);

Portability

DOSUNIXWindowsANSI CC++ only
■■■■

Remarks

setbuf causes the buffer buf to be used for I/O buffering instead of an automatically allocated buffer. It is used after stream has been opened.

If buf is null, I/O will be unbuffered; otherwise, it will be fully buffered. The buffer must be BUFSIZ bytes long (specified in stdio.h).

stdin and stdout are unbuffered if they are not redirected; otherwise, they are fully buffered, setbuf can be used to change the buffering style being used.

Unbuffered means that characters written to a stream are immediately output to the file or device, while buffered means that the characters are accumulated and written as a block.

setbuf produces unpredictable results unless it is called immediately after opening stream or after a call to fseek. Calling setbuf after stream has been unbuffered is legal and will not cause problems.

A common cause for error is to allocate the buffer as an automatic (local) variable and then fail to close the file before returning from the function where the buffer was declared.

Return value

None.

See also

Example

#include <stdio.h>

/* bufsiz is defined  in stdio.h */
char outbuf [BUFSIZ];
int main(void)
{
   /* attach buffer to standard  output stream */
   setbuf(stdout, outbuf);

   /* put some characters  into the buffer */
   puts("This is a test of buffered  output.\n\n")
   puts("This output will go  into outbuf\n"),•
   puts("and won't appear until  the buffer\n");
   puts("fills up or we flush  the stream.\n");

   /* flush the output buffer  7
   fflush(stdout);
   return 0;

Differences from modern implementations

Nothing recorded yet.

Modern references

These are search links, not yet checked by hand.