.\" Copyright (c) 1980 Regents of the University of California. .\" All rights reserved. The Berkeley software License Agreement .\" specifies the terms and conditions for redistribution. .\" .\" @(#)setbuf.3 6.4 (Berkeley) 4/1/89 .\" .TH SETBUF 3 "April 1, 1989" .UC 4 .SH NAME setbuf, setbuffer, setlinebuf \- assign buffering to a stream .SH SYNOPSIS .B #include .PP .B setbuf(stream, buf) .br .SM .B FILE .B *stream; .br .B char *buf; .PP .B setbuffer(stream, buf, size) .br .SM .B FILE .B *stream; .br .B char *buf; .br .B int size; .PP .B setlinebuf(stream) .br .SM .B FILE .B *stream; .SH DESCRIPTION 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 up and written as a block; when it is line buffered characters are saved up until a newline is encountered or input is read from stdin. .I Fflush (see .IR fclose (3)) may be used to force the block out early. Normally all files are block buffered. A buffer is obtained from .IR malloc (3) upon the first .I getc or .IR putc (3) on the file. If the standard stream .B stdout refers to a terminal it is line buffered. The standard stream .B stderr is always unbuffered. .PP .I Setbuf is used after a stream has been opened but before it is read or written. The character array .I buf is used instead of an automatically allocated buffer. If .I buf is the constant pointer .SM .B NULL, input/output will be completely unbuffered. A manifest constant .SM .B BUFSIZ tells how big an array is needed: .IP .B char buf[BUFSIZ]; .PP .IR Setbuffer , an alternate form of .IR setbuf , is used after a stream has been opened but before it is read or written. The character array .I buf whose size is determined by the .I size argument is used instead of an automatically allocated buffer. If .I buf is the constant pointer .SM .BR NULL , input/output will be completely unbuffered. .PP .I Setlinebuf is used to change .I stdout or .I stderr from block buffered or unbuffered to line buffered. Unlike .I setbuf and .I setbuffer it can be used at any time that the file descriptor is active. .PP A file can be changed from unbuffered or line buffered to block buffered by using .I freopen (see .IR fopen (3)). A file can be changed from block buffered or line buffered to unbuffered by using .I freopen followed by .I setbuf with a buffer argument of .SM .BR NULL . .SH "SEE ALSO" fopen(3), getc(3), putc(3), malloc(3), fclose(3), puts(3), printf(3), fread(3) .SH BUGS The .I setbuffer and .I setlinebuf functions are not portable to non-4.2BSD versions of UNIX. On 4.2BSD and 4.3BSD systems, .I setbuf always uses a suboptimal buffer size and should be avoided. .I Setbuffer is not usually needed as the default file I/O buffer sizes are optimal.