1SETBUF(3)                  Linux Programmer's Manual                 SETBUF(3)
2
3
4

NAME

6       setbuf, setbuffer, setlinebuf, setvbuf - stream buffering operations
7

SYNOPSIS

9       #include <stdio.h>
10
11       int setvbuf(FILE *restrict stream, char *restrict buf,
12                   int mode, size_t size);
13
14       void setbuf(FILE *restrict stream, char *restrict buf);
15       void setbuffer(FILE *restrict stream, char *restrict buf,
16                   size_t size);
17       void setlinebuf(FILE *stream);
18
19   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
20
21       setbuffer(), setlinebuf():
22           Since glibc 2.19:
23               _DEFAULT_SOURCE
24           Glibc 2.19 and earlier:
25               _BSD_SOURCE
26

DESCRIPTION

28       The  three types of buffering available are unbuffered, block buffered,
29       and line buffered.  When an output stream  is  unbuffered,  information
30       appears on the destination file or terminal as soon as written; when it
31       is block buffered, many characters are saved up and written as a block;
32       when  it  is  line buffered, characters are saved up until a newline is
33       output or input is read from any stream attached to a  terminal  device
34       (typically  stdin).   The  function  fflush(3) may be used to force the
35       block out early.  (See fclose(3).)
36
37       Normally all files are block buffered.  If a stream refers to a  termi‐
38       nal (as stdout normally does), it is line buffered.  The standard error
39       stream stderr is always unbuffered by default.
40
41       The setvbuf() function may be used on any open  stream  to  change  its
42       buffer.  The mode argument must be one of the following three macros:
43
44              _IONBF unbuffered
45
46              _IOLBF line buffered
47
48              _IOFBF fully buffered
49
50       Except  for unbuffered files, the buf argument should point to a buffer
51       at least size bytes long; this buffer will be used instead of the  cur‐
52       rent buffer.  If the argument buf is NULL, only the mode is affected; a
53       new buffer will be allocated on the next read or write operation.   The
54       setvbuf()  function  may be used only after opening a stream and before
55       any other operations have been performed on it.
56
57       The other three calls are, in  effect,  simply  aliases  for  calls  to
58       setvbuf().  The setbuf() function is exactly equivalent to the call
59
60           setvbuf(stream, buf, buf ? _IOFBF : _IONBF, BUFSIZ);
61
62       The  setbuffer() function is the same, except that the size of the buf‐
63       fer is up to the caller, rather than being determined  by  the  default
64       BUFSIZ.  The setlinebuf() function is exactly equivalent to the call:
65
66           setvbuf(stream, NULL, _IOLBF, 0);
67

RETURN VALUE

69       The  function  setvbuf()  returns  0 on success.  It returns nonzero on
70       failure (mode is invalid or the request cannot be honored).  It may set
71       errno on failure.
72
73       The other functions do not return a value.
74

ATTRIBUTES

76       For  an  explanation  of  the  terms  used  in  this  section,  see at‐
77       tributes(7).
78
79       ┌────────────────────────────────────────────┬───────────────┬─────────┐
80Interface                                   Attribute     Value   
81       ├────────────────────────────────────────────┼───────────────┼─────────┤
82setbuf(), setbuffer(), setlinebuf(),        │ Thread safety │ MT-Safe │
83setvbuf()                                   │               │         │
84       └────────────────────────────────────────────┴───────────────┴─────────┘
85

CONFORMING TO

87       The setbuf() and setvbuf() functions conform to C89 and C99.
88

NOTES

90       POSIX notes that the value of errno is unspecified after a call to set‐
91       buf() and further notes that, since the value of errno is not  required
92       to  be  unchanged  after  a  successful  call to setbuf(), applications
93       should instead use setvbuf() in order to detect errors.
94

BUGS

96       You must make sure that the space that buf points to  still  exists  by
97       the  time  stream is closed, which also happens at program termination.
98       For example, the following is invalid:
99
100       #include <stdio.h>
101
102       int
103       main(void)
104       {
105           char buf[BUFSIZ];
106           setbuf(stdout, buf);
107           printf("Hello, world!\n");
108           return 0;
109       }
110

SEE ALSO

112       stdbuf(1),  fclose(3),  fflush(3),   fopen(3),   fread(3),   malloc(3),
113       printf(3), puts(3)
114

COLOPHON

116       This  page  is  part of release 5.12 of the Linux man-pages project.  A
117       description of the project, information about reporting bugs,  and  the
118       latest     version     of     this    page,    can    be    found    at
119       https://www.kernel.org/doc/man-pages/.
120
121
122
123Linux                             2021-03-22                         SETBUF(3)
Impressum