1setbuf(3C) Standard C Library Functions setbuf(3C)
2
3
4
6 setbuf, setvbuf - assign buffering to a stream
7
9 #include <stdio.h>
10
11 void setbuf(FILE *stream, char *buf);
12
13
14 int setvbuf(FILE *stream, char *buf, int type, size_t size);
15
16
18 The setbuf() function may be used after the stream pointed to by stream
19 (see Intro(3)) is opened but before it is read or written. It causes
20 the array pointed to by buf to be used instead of an automatically
21 allocated buffer. If buf is the null pointer, input/output will be com‐
22 pletely unbuffered. The constant BUFSIZ, defined in the <stdio.h>
23 header, indicates the size of the array pointed to by buf.
24
25
26 The setvbuf() function may be used after a stream is opened but before
27 it is read or written. The type argument determines how stream will
28 be buffered. Legal values for type (defined in <stdio.h>) are:
29
30 _IOFBF Input/output to be fully buffered.
31
32
33 _IOLBF Output to be line buffered; the buffer will be flushed when
34 a NEWLINE is written, the buffer is full, or input is
35 requested.
36
37
38 _IONBF Input/output to be completely unbuffered.
39
40
41
42 If buf is not the null pointer, the array it points to will be used for
43 buffering, instead of an automatically allocated buffer. The size
44 argument specifies the size of the buffer to be used. If input/output
45 is unbuffered, buf and size are ignored.
46
47
48 For a further discussion of buffering, see stdio(3C).
49
51 If an illegal value for type is provided, setvbuf() returns a non-zero
52 value. Otherwise, it returns 0.
53
55 A common source of error is allocating buffer space as an "automatic"
56 variable in a code block, and then failing to close the stream in the
57 same block.
58
59
60 When using setbuf(), buf should always be sized using BUFSIZ. If the
61 array pointed to by buf is larger than BUFSIZ, a portion of buf will
62 not be used. If buf is smaller than BUFSIZ, other memory may be unex‐
63 pectedly overwritten.
64
65
66 Parts of buf will be used for internal bookkeeping of the stream and,
67 therefore, buf will contain less than size bytes when full. It is rec‐
68 ommended that stdio(3C) be used to handle buffer allocation when using
69 setvbuf().
70
72 See attributes(5) for descriptions of the following attributes:
73
74
75
76
77 ┌─────────────────────────────┬─────────────────────────────┐
78 │ ATTRIBUTE TYPE │ ATTRIBUTE VALUE │
79 ├─────────────────────────────┼─────────────────────────────┤
80 │Interface Stability │Standard │
81 ├─────────────────────────────┼─────────────────────────────┤
82 │MT-Level │MT-Safe │
83 └─────────────────────────────┴─────────────────────────────┘
84
86 fopen(3C), getc(3C), malloc(3C), putc(3C), stdio(3C), attributes(5),
87 standards(5)
88
89
90
91SunOS 5.11 14 Aug 2002 setbuf(3C)