1SETBUF(3) Linux Programmer's Manual SETBUF(3)
2
3
4
6 setbuf, setbuffer, setlinebuf, setvbuf - stream buffering operations
7
9 #include <stdio.h>
10
11 void setbuf(FILE *stream, char *buf);
12 void setbuffer(FILE *stream, char *buf, size_t size);
13 void setlinebuf(FILE *stream);
14 int setvbuf(FILE *stream, char *buf, int mode , size_t size);
15
17 The three types of buffering available are unbuffered, block buffered,
18 and line buffered. When an output stream is unbuffered, information
19 appears on the destination file or terminal as soon as written; when it
20 is block buffered many characters are saved up and written as a block;
21 when it is line buffered characters are saved up until a newline is
22 output or input is read from any stream attached to a terminal device
23 (typically stdin). The function fflush(3) may be used to force the
24 block out early. (See fclose(3).) Normally all files are block
25 buffered. When the first I/O operation occurs on a file, malloc(3) is
26 called, and a buffer is obtained. If a stream refers to a terminal (as
27 stdout normally does) it is line buffered. The standard error stream
28 stderr is always unbuffered by default.
29
30 The setvbuf() function may be used on any open stream to change its
31 buffer. The mode parameter must be one of the following three macros:
32
33 _IONBF unbuffered
34
35 _IOLBF line buffered
36
37 _IOFBF fully buffered
38
39 Except for unbuffered files, the buf argument should point to a buffer
40 at least size bytes long; this buffer will be used instead of the cur‐
41 rent buffer. If the argument buf is NULL, only the mode is affected; a
42 new buffer will be allocated on the next read or write operation. The
43 setvbuf() function may only be used after opening a stream and before
44 any other operations have been performed on it.
45
46 The other three calls are, in effect, simply aliases for calls to
47 setvbuf(). The setbuf() function is exactly equivalent to the call
48
49 setvbuf(stream, buf, buf ? _IOFBF : _IONBF, BUFSIZ);
50
51 The setbuffer() function is the same, except that the size of the buf‐
52 fer is up to the caller, rather than being determined by the default
53 BUFSIZ. The setlinebuf() function is exactly equivalent to the call:
54
55 setvbuf(stream, (char *)NULL, _IOLBF, 0);
56
58 The function setvbuf() returns 0 on success. It can return any value
59 on failure, but returns non-zero when mode is invalid or the request
60 cannot be honoured. It may set errno on failure. The other functions
61 are void.
62
64 The setbuf() and setvbuf() functions conform to C89 and C99.
65
67 The setbuffer() and setlinebuf() functions are not portable to versions
68 of BSD before 4.2BSD, and are available under Linux since libc 4.5.21.
69 On 4.2BSD and 4.3BSD systems, setbuf() always uses a suboptimal buffer
70 size and should be avoided.
71
72 You must make sure that both buf and the space it points to still exist
73 by the time stream is closed, which also happens at program termina‐
74 tion.
75
76 For example, the following is illegal:
77
78 #include <stdio.h>
79 int main()
80 {
81 char buf[BUFSIZ];
82 setbuf(stdin, buf);
83 printf("Hello, world!\n");
84 return 0;
85 }
86
87
89 fclose(3), fflush(3), fopen(3), fread(3), malloc(3), printf(3), puts(3)
90
91
92
93Linux 2001-06-09 SETBUF(3)