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       void setbuf(FILE *stream, char *buf);
12
13       void setbuffer(FILE *stream, char *buf, size_t size);
14
15       void setlinebuf(FILE *stream);
16
17       int setvbuf(FILE *stream, char *buf, int mode, size_t size);
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
77       attributes(7).
78
79       ┌────────────────────────┬───────────────┬─────────┐
80Interface               Attribute     Value   
81       ├────────────────────────┼───────────────┼─────────┤
82setbuf(), setbuffer(),  │ Thread safety │ MT-Safe │
83setlinebuf(), setvbuf() │               │         │
84       └────────────────────────┴───────────────┴─────────┘

CONFORMING TO

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

BUGS

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

SEE ALSO

105       stdbuf(1),  fclose(3),  fflush(3),   fopen(3),   fread(3),   malloc(3),
106       printf(3), puts(3)
107

COLOPHON

109       This  page  is  part of release 5.07 of the Linux man-pages project.  A
110       description of the project, information about reporting bugs,  and  the
111       latest     version     of     this    page,    can    be    found    at
112       https://www.kernel.org/doc/man-pages/.
113
114
115
116Linux                             2019-03-06                         SETBUF(3)
Impressum