1setbuf(3) Library Functions Manual setbuf(3)
2
3
4
6 setbuf, setbuffer, setlinebuf, setvbuf - stream buffering operations
7
9 Standard C library (libc, -lc)
10
12 #include <stdio.h>
13
14 int setvbuf(FILE *restrict stream, char buf[restrict .size],
15 int mode, size_t size);
16
17 void setbuf(FILE *restrict stream, char *restrict buf);
18 void setbuffer(FILE *restrict stream, char buf[restrict .size],
19 size_t size);
20 void setlinebuf(FILE *stream);
21
22 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
23
24 setbuffer(), setlinebuf():
25 Since glibc 2.19:
26 _DEFAULT_SOURCE
27 glibc 2.19 and earlier:
28 _BSD_SOURCE
29
31 The three types of buffering available are unbuffered, block buffered,
32 and line buffered. When an output stream is unbuffered, information
33 appears on the destination file or terminal as soon as written; when it
34 is block buffered, many characters are saved up and written as a block;
35 when it is line buffered, characters are saved up until a newline is
36 output or input is read from any stream attached to a terminal device
37 (typically stdin). The function fflush(3) may be used to force the
38 block out early. (See fclose(3).)
39
40 Normally all files are block buffered. If a stream refers to a termi‐
41 nal (as stdout normally does), it is line buffered. The standard error
42 stream stderr is always unbuffered by default.
43
44 The setvbuf() function may be used on any open stream to change its
45 buffer. The mode argument must be one of the following three macros:
46
47 _IONBF unbuffered
48
49 _IOLBF line buffered
50
51 _IOFBF fully buffered
52
53 Except for unbuffered files, the buf argument should point to a buffer
54 at least size bytes long; this buffer will be used instead of the cur‐
55 rent buffer. If the argument buf is NULL, only the mode is affected; a
56 new buffer will be allocated on the next read or write operation. The
57 setvbuf() function may be used only after opening a stream and before
58 any other operations have been performed on it.
59
60 The other three calls are, in effect, simply aliases for calls to
61 setvbuf(). The setbuf() function is exactly equivalent to the call
62
63 setvbuf(stream, buf, buf ? _IOFBF : _IONBF, BUFSIZ);
64
65 The setbuffer() function is the same, except that the size of the buf‐
66 fer is up to the caller, rather than being determined by the default
67 BUFSIZ. The setlinebuf() function is exactly equivalent to the call:
68
69 setvbuf(stream, NULL, _IOLBF, 0);
70
72 The function setvbuf() returns 0 on success. It returns nonzero on
73 failure (mode is invalid or the request cannot be honored). It may set
74 errno on failure.
75
76 The other functions do not return a value.
77
79 For an explanation of the terms used in this section, see at‐
80 tributes(7).
81
82 ┌────────────────────────────────────────────┬───────────────┬─────────┐
83 │Interface │ Attribute │ Value │
84 ├────────────────────────────────────────────┼───────────────┼─────────┤
85 │setbuf(), setbuffer(), setlinebuf(), │ Thread safety │ MT-Safe │
86 │setvbuf() │ │ │
87 └────────────────────────────────────────────┴───────────────┴─────────┘
88
90 setbuf()
91 setvbuf()
92 C11, POSIX.1-2008.
93
95 setbuf()
96 setvbuf()
97 C89, POSIX.1-2001.
98
100 POSIX notes that the value of errno is unspecified after a call to set‐
101 buf() and further notes that, since the value of errno is not required
102 to be unchanged after a successful call to setbuf(), applications
103 should instead use setvbuf() in order to detect errors.
104
106 You must make sure that the space that buf points to still exists by
107 the time stream is closed, which also happens at program termination.
108 For example, the following is invalid:
109
110 #include <stdio.h>
111
112 int
113 main(void)
114 {
115 char buf[BUFSIZ];
116
117 setbuf(stdout, buf);
118 printf("Hello, world!\n");
119 return 0;
120 }
121
123 stdbuf(1), fclose(3), fflush(3), fopen(3), fread(3), malloc(3),
124 printf(3), puts(3)
125
126
127
128Linux man-pages 6.04 2023-03-30 setbuf(3)