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