'\" te .\" Copyright (c) 1997, Sun Microsystems, Inc. All Rights Reserved .TH setbuffer 3C "13 May 1997" "SunOS 5.11" "Standard C Library Functions" .SH NAME setbuffer, setlinebuf \- assign buffering to a stream .SH SYNOPSIS .LP .nf #include \fBvoid\fR \fBsetbuffer\fR(\fBFILE *\fR\fIiop\fR, \fBchar *\fR\fIabuf\fR, \fBsize_t\fR \fIasize\fR); .fi .LP .nf \fBint\fR \fBsetlinebuf\fR(\fBFILE *\fR\fIiop\fR); .fi .SH DESCRIPTION .sp .LP The \fBsetbuffer()\fR and \fBsetlinebuf()\fR functions assign buffering to a stream. The three types of buffering available are unbuffered, block buffered, and line buffered. When an output stream is unbuffered, information appears on the destination file or terminal as soon as written; when it is block buffered, many characters are saved and written as a block; when it is line buffered, characters are saved until either a \fBNEWLINE\fR is encountered or input is read from \fBstdin\fR. The \fBfflush\fR(3C) function may be used to force the block out early. Normally all files are block buffered. A buffer is obtained from \fBmalloc\fR(3C) upon the first \fBgetc\fR(3C) or \fBputc\fR(3C) performed on the file. If the standard stream \fBstdout\fR refers to a terminal, it is line buffered. The standard stream \fBstderr\fR is unbuffered by default. .sp .LP The \fBsetbuffer()\fR function can be used after a stream \fIiop\fR has been opened but before it is read or written. It uses the character array \fIabuf\fR whose size is determined by the \fIasize\fR argument instead of an automatically allocated buffer. If \fIabuf\fR is the null pointer, input/output will be completely unbuffered. A manifest constant \fBBUFSIZ\fR, defined in the \fB\fR header, tells how large an array is needed: .sp .LP char buf[BUFSIZ]; .sp .LP The \fBsetlinebuf()\fR function is used to change the buffering on a stream from block buffered or unbuffered to line buffered. Unlike \fBsetbuffer()\fR, it can be used at any time that the stream \fIiop\fR is active. .sp .LP A stream can be changed from unbuffered or line buffered to block buffered by using \fBfreopen\fR(3C). A stream can be changed from block buffered or line buffered to unbuffered by using \fBfreopen\fR(3C) followed by \fBsetbuf\fR(3C) with a buffer argument of \fINULL\fR. .SH RETURN VALUES .sp .LP The \fBsetlinebuf()\fR function returns no useful value. .SH SEE ALSO .sp .LP \fBmalloc\fR(3C), \fBfclose\fR(3C), \fBfopen\fR(3C), \fBfread\fR(3C), \fBgetc\fR(3C), \fBprintf\fR(3C), \fBputc\fR(3C), \fBputs\fR(3C), \fBsetbuf\fR(3C), \fBsetvbuf\fR(3C) .SH NOTES .sp .LP A common source of error is allocating buffer space as an "automatic" variable in a code block, and then failing to close the stream in the same block.