Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

setbuf - assign buffering to stream

&pagelevel(4)&pagelevel

Syntax

#include <stdio.h>

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

Description

setbuf() may be used after the stream pointed to by stream has been assigned to an open
file but before any other operation has been performed on the stream. It causes the array
pointed to by buf to be used instead of an automatically allocated buffer.

The buffer size is not limited; however, the constant BUFSIZ (see stdio.h) is typically a
good buffer size:

char buf[BUFSIZ];

If buf is not a null pointer, the following function calls are equivalent:

setbuf(stream, buf)
setvbuf(stream, buf, _IOFBF, BUFSIZ)

If buf is a null pointer, input and output are unbuffered, and the following calls are equivalent:

setbuf(stream, buf)
setvbuf(stream, buf, _IONBF, BUFSIZ)

BS2000
If buf is a null pointer, the buffer assigned by the system is used. (End)

In contrast to setvbuf(), setbuf() has no return value.

Notes

A common source of error is to use an "automatic" variable (i.e. a variable of storage class
auto) as the buffer in a program block and then fail to close the file in the same block.

Since a portion of buf is required for internal administration data of the stream, buf will
contain less than size bytes when full. It is therefore preferable to use setvbuf() with
automatically assigned buffers.

setbuf() is executed for the file assigned to stream. This can be a POSIX file or a
BS2000 file.

BS2000
If the blocking factor is explicitly defined with the BUFFER-LENGTH parameter of the
SET-FILE-LINK command, the size of the area must correspond to this defined blocking
size. (End)

See also

fopen(), setvbuf(), stdio.h, section “Streams”.