1 | .\" Copyright (c) 1980 Regents of the University of California.
|
---|
2 | .\" All rights reserved. The Berkeley software License Agreement
|
---|
3 | .\" specifies the terms and conditions for redistribution.
|
---|
4 | .\"
|
---|
5 | .\" @(#)setbuf.3s 6.2 (Berkeley) 5/12/86
|
---|
6 | .\"
|
---|
7 | .TH SETBUF 3 "May 12, 1986"
|
---|
8 | .UC 4
|
---|
9 | .SH NAME
|
---|
10 | setbuf, setvbuf \- assign buffering to a stream
|
---|
11 | .SH SYNOPSIS
|
---|
12 | .nf
|
---|
13 | .ft B
|
---|
14 | #include <stdio.h>
|
---|
15 |
|
---|
16 | int setbuf(FILE *\fIstream\fP, char *\fIbuf\fP)
|
---|
17 | int setvbuf(FILE *\fIstream\fP, char *\fIbuf\fP, int \fItype\fP, size_t \fIsize\fP)
|
---|
18 | .SH DESCRIPTION
|
---|
19 | The three types of buffering available are unbuffered, block buffered,
|
---|
20 | and line buffered.
|
---|
21 | When an output stream is unbuffered, information appears on the
|
---|
22 | destination file or terminal as soon as written;
|
---|
23 | when it is block buffered many characters are saved up and written as a block;
|
---|
24 | when it is line buffered characters are saved up until a newline is
|
---|
25 | encountered or input is read from stdin.
|
---|
26 | .B Fflush
|
---|
27 | (see
|
---|
28 | .BR fclose (3))
|
---|
29 | may be used to force the block out early.
|
---|
30 | Normally all files are block buffered.
|
---|
31 | A buffer is obtained from
|
---|
32 | .BR malloc (3)
|
---|
33 | upon the first
|
---|
34 | .B getc
|
---|
35 | or
|
---|
36 | .BR putc (3)
|
---|
37 | on the file.
|
---|
38 | If the standard stream
|
---|
39 | .B stdout
|
---|
40 | refers to a terminal it is line buffered.
|
---|
41 | The standard stream
|
---|
42 | .B stderr
|
---|
43 | is always unbuffered.
|
---|
44 | .PP
|
---|
45 | .B Setbuf
|
---|
46 | is used after a stream has been opened but before it is read or written.
|
---|
47 | The character array
|
---|
48 | .I buf
|
---|
49 | is used instead of an automatically allocated buffer. If
|
---|
50 | .I buf
|
---|
51 | is the constant pointer
|
---|
52 | .SM
|
---|
53 | .BR NULL ,
|
---|
54 | input/output will be completely unbuffered.
|
---|
55 | A manifest constant
|
---|
56 | .SM
|
---|
57 | .B BUFSIZ
|
---|
58 | tells how big an array is needed:
|
---|
59 | .IP
|
---|
60 | .B char
|
---|
61 | buf[BUFSIZ];
|
---|
62 | .PP
|
---|
63 | .BR Setvbuf ,
|
---|
64 | an alternate form of
|
---|
65 | .BR setbuf ,
|
---|
66 | is used after a stream has been opened but before it is read or written.
|
---|
67 | It has three uses, depending on the value of the
|
---|
68 | .IR type
|
---|
69 | argument:
|
---|
70 | .TP 5
|
---|
71 | .B "setvbuf(\fIstream\fP, \fIbuf\fP, _IOFBF, \fIsize\fP)"
|
---|
72 | Causes input/output to be fully buffered using the character array
|
---|
73 | .I buf
|
---|
74 | whose size is determined by the
|
---|
75 | .I size
|
---|
76 | argument.
|
---|
77 | If
|
---|
78 | .I buf
|
---|
79 | is the constant pointer
|
---|
80 | .SM
|
---|
81 | .BR NULL ,
|
---|
82 | then an automatically allocated buffer will be used.
|
---|
83 | .TP 5
|
---|
84 | .B "setvbuf(\fIstream\fP, \fIbuf\fP, _IOLBF, \fIsize\fP)"
|
---|
85 | Like above, except that output will be line buffered, i.e. the buffer will
|
---|
86 | be flushed when a newline is written, the buffer is full, or input is
|
---|
87 | requested.
|
---|
88 | .TP 5
|
---|
89 | .B "setvbuf(\fIstream\fP, \fIbuf\fP, _IONBF, \fIsize\fP)"
|
---|
90 | Causes input/output to be completely unbuffered.
|
---|
91 | .I Buf
|
---|
92 | and
|
---|
93 | .I size
|
---|
94 | are ignored.
|
---|
95 | .PP
|
---|
96 | A file can be changed between unbuffered, line buffered, or block buffered
|
---|
97 | by using
|
---|
98 | .B freopen
|
---|
99 | (see
|
---|
100 | .BR fopen (3))
|
---|
101 | followed by the appropriate
|
---|
102 | .B setvbuf
|
---|
103 | call.
|
---|
104 | .SH "SEE ALSO"
|
---|
105 | .BR fopen (3),
|
---|
106 | .BR getc (3),
|
---|
107 | .BR putc (3),
|
---|
108 | .BR malloc (3),
|
---|
109 | .BR fclose (3),
|
---|
110 | .BR puts (3),
|
---|
111 | .BR printf (3),
|
---|
112 | .BR fread (3).
|
---|