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(): _BSD_SOURCE
22

DESCRIPTION

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

RETURN VALUE

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

CONFORMING TO

72       The setbuf() and setvbuf() functions conform to C89 and C99.
73

BUGS

75       The setbuffer() and setlinebuf() functions are not portable to versions
76       of BSD before 4.2BSD, and are available under Linux since libc  4.5.21.
77       On  4.2BSD and 4.3BSD systems, setbuf() always uses a suboptimal buffer
78       size and should be avoided.
79
80       You must make sure that the space that buf points to  still  exists  by
81       the  time  stream is closed, which also happens at program termination.
82       For example, the following is invalid:
83
84       #include <stdio.h>
85
86       int
87       main(void)
88       {
89           char buf[BUFSIZ];
90           setbuf(stdin, buf);
91           printf("Hello, world!\n");
92           return 0;
93       }
94

SEE ALSO

96       fclose(3), fflush(3), fopen(3), fread(3), malloc(3), printf(3), puts(3)
97

COLOPHON

99       This page is part of release 3.53 of the Linux  man-pages  project.   A
100       description  of  the project, and information about reporting bugs, can
101       be found at http://www.kernel.org/doc/man-pages/.
102
103
104
105Linux                             2012-08-03                         SETBUF(3)
Impressum