xref: /freebsd/lib/libc/stdio/stdio.3 (revision e74101e4eff767325553039def89de70b70f36d3)
1.\" Copyright (c) 1990, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. All advertising materials mentioning features or use of this software
13.\"    must display the following acknowledgement:
14.\"	This product includes software developed by the University of
15.\"	California, Berkeley and its contributors.
16.\" 4. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\"     @(#)stdio.3	8.7 (Berkeley) 4/19/94
33.\" $FreeBSD$
34.\"
35.Dd August 11, 2002
36.Dt STDIO 3
37.Os
38.Sh NAME
39.Nm stdio
40.Nd standard input/output library functions
41.Sh LIBRARY
42.Lb libc
43.Sh SYNOPSIS
44.In stdio.h
45.Vt FILE *stdin ;
46.Vt FILE *stdout ;
47.Vt FILE *stderr ;
48.Sh DESCRIPTION
49The standard
50.Tn I/O
51library provides a simple and efficient buffered stream
52.Tn I/O
53interface.
54Input and output is mapped into logical data streams
55and the physical
56.Tn I/O
57characteristics are concealed.
58The functions and macros are listed
59below; more information is available from the individual man pages.
60.Pp
61A stream is associated with an external file (which may be a physical
62device) by
63.Em opening
64a file, which may involve creating a new file.
65Creating an
66existing file causes its former contents to be discarded.
67If a file can support positioning requests (such as a disk file, as opposed
68to a terminal) then a
69.Em file position indicator
70associated with the stream is positioned at the start of the file (byte
71zero), unless the file is opened with append mode.
72If append mode
73is used, the position indicator will be placed at the end-of-file.
74The position indicator is maintained by subsequent reads, writes
75and positioning requests.
76All input occurs as if the characters
77were read by successive calls to the
78.Xr fgetc 3
79function; all output takes place as if all characters were
80written by successive calls to the
81.Xr fputc 3
82function.
83.Pp
84A file is disassociated from a stream by
85.Em closing
86the file.
87Output streams are flushed (any unwritten buffer contents are transferred
88to the host environment) before the stream is disassociated from the file.
89The value of a pointer to a
90.Dv FILE
91object is indeterminate (garbage) after a file is closed.
92.Pp
93A file may be subsequently reopened, by the same or another program
94execution, and its contents reclaimed or modified (if it can be repositioned
95at the start).  If the main function returns to its original caller, or
96the
97.Xr exit 3
98function is called, all open files are closed (hence all output
99streams are flushed) before program termination.  Other methods
100of program termination may not close files properly and hence
101buffered output may be lost.  In particular,
102.Xr _exit 2
103does not flush stdio files.  Neither does an exit due to a signal.
104Buffers are flushed by
105.Xr abort 3
106as required by POSIX, although previous implementations did not.
107.Pp
108This implementation makes no distinction between
109.Dq text
110and
111.Dq binary
112streams.
113In effect, all streams are binary.
114No translation is performed and no extra padding appears on any stream.
115.Pp
116At program startup, three streams are predefined and need not be
117opened explicitly:
118.Bl -bullet -compact -offset indent
119.It
120.Em standard input
121(for reading conventional input),
122.It
123.Em standard output
124(for writing conventional output), and
125.It
126.Em standard error
127(for writing diagnostic output).
128.El
129These streams are abbreviated
130.Em stdin , stdout
131and
132.Em stderr .
133Initially, the standard error stream
134is unbuffered; the standard input and output streams are
135fully buffered if and only if the streams do not refer to
136an interactive or
137.Dq terminal
138device, as determined by the
139.Xr isatty 3
140function.
141In fact,
142.Em all
143freshly-opened streams that refer to terminal devices
144default to line buffering, and
145pending output to such streams is written automatically
146whenever such an input stream is read.
147Note that this applies only to
148.Dq "true reads" ;
149if the read request can be satisfied by existing buffered data,
150no automatic flush will occur.
151In these cases,
152or when a large amount of computation is done after printing
153part of a line on an output terminal, it is necessary to
154.Xr fflush 3
155the standard output before going off and computing so that the output
156will appear.
157Alternatively, these defaults may be modified via the
158.Xr setvbuf 3
159function.
160.Pp
161The
162.Nm
163library is a part of the library
164.Nm libc
165and routines are automatically loaded as needed by the C compiler.
166The
167.Tn SYNOPSIS
168sections of the following manual pages indicate which include files
169are to be used, what the compiler declaration for the function
170looks like and which external variables are of interest.
171.Pp
172The following are defined as macros;
173these names may not be re-used
174without first removing their current definitions with
175.Dv #undef :
176.Dv BUFSIZ ,
177.Dv EOF ,
178.Dv FILENAME_MAX ,
179.Dv FOPEN_MAX ,
180.Dv L_cuserid ,
181.Dv L_ctermid ,
182.Dv L_tmpnam ,
183.Dv NULL ,
184.Dv P_tmpdir ,
185.Dv SEEK_CUR ,
186.Dv SEEK_END ,
187.Dv SEEK_SET ,
188.Dv TMP_MAX ,
189.Dv clearerr ,
190.Dv feof ,
191.Dv ferror ,
192.Dv fileno ,
193.Dv fropen ,
194.Dv fwopen ,
195.Dv getc ,
196.Dv getchar ,
197.Dv getwc ,
198.Dv getwchar ,
199.Dv putc ,
200.Dv putchar ,
201.Dv putwc ,
202.Dv putwchar ,
203.Dv stderr ,
204.Dv stdin ,
205.Dv stdout ,
206.Dv vfscanf .
207Function versions of the macro functions
208.Fn clearerr ,
209.Fn feof ,
210.Fn ferror ,
211.Fn fileno ,
212.Fn getc ,
213.Fn getchar ,
214.Fn getwc ,
215.Fn getwchar ,
216.Fn putc ,
217.Fn putchar ,
218.Fn putwc
219and
220.Fn putwchar
221exist and will be used if the macro
222definitions are explicitly removed.
223.Sh SEE ALSO
224.Xr close 2 ,
225.Xr open 2 ,
226.Xr read 2 ,
227.Xr write 2
228.Sh BUGS
229The standard buffered functions do not interact well with certain other
230library and system functions, especially
231.Xr vfork 2 .
232.Sh STANDARDS
233The
234.Nm
235library conforms to
236.St -isoC-99 .
237.Sh LIST OF FUNCTIONS
238.Bl -column "Description"
239.It Sy "Function	Description"
240.It "asprintf	formatted output conversion"
241.It "clearerr	check and reset stream status"
242.It "fclose	close a stream"
243.It "fdopen	stream open functions"
244.It "feof	check and reset stream status"
245.It "ferror	check and reset stream status"
246.It "fflush	flush a stream"
247.It "fgetc	get next character or word from input stream"
248.It "fgetln	get a line from a stream"
249.It "fgetpos	reposition a stream"
250.It "fgets	get a line from a stream"
251.It "fgetwc	get next wide character from input stream"
252.It "fgetws	get a line of wide characters from a stream"
253.It "fileno	check and reset stream status"
254.It "fopen	stream open functions"
255.It "fprintf	formatted output conversion"
256.It "fpurge	flush a stream"
257.It "fputc	output a character or word to a stream"
258.It "fputs	output a line to a stream"
259.It "fputwc	output a wide character to a stream"
260.It "fputws	output a line of wide characters to a stream"
261.It "fread	binary stream input/output"
262.It "freopen	stream open functions"
263.It "fropen	open a stream"
264.It "fscanf	input format conversion"
265.It "fseek	reposition a stream"
266.It "fsetpos	reposition a stream"
267.It "ftell	reposition a stream"
268.It "funopen	open a stream"
269.It "fwide	set/get orientation of stream"
270.It "fwopen	open a stream"
271.It "fwrite	binary stream input/output"
272.It "getc	get next character or word from input stream"
273.It "getchar	get next character or word from input stream"
274.It "gets	get a line from a stream"
275.It "getw	get next character or word from input stream"
276.It "getwc	get next wide character from input stream"
277.It "getwchar	get next wide character from input stream"
278.It "mkdtemp	create unique temporary file"
279.It "mkstemp	create unique temporary file"
280.It "mktemp	create unique temporary file"
281.It "perror	system error messages"
282.It "printf	formatted output conversion"
283.It "putc	output a character or word to a stream"
284.It "putchar	output a character or word to a stream"
285.It "puts	output a line to a stream"
286.It "putw	output a character or word to a stream"
287.It "putwc	output a wide character to a stream"
288.It "putwchar	output a wide character to a stream"
289.It "remove	remove directory entry"
290.It "rewind	reposition a stream"
291.It "scanf	input format conversion"
292.It "setbuf	stream buffering operations"
293.It "setbuffer	stream buffering operations"
294.It "setlinebuf	stream buffering operations"
295.It "setvbuf	stream buffering operations"
296.It "snprintf	formatted output conversion"
297.It "sprintf	formatted output conversion"
298.It "sscanf	input format conversion"
299.It "strerror	system error messages"
300.It "sys_errlist	system error messages"
301.It "sys_nerr	system error messages"
302.It "tempnam	temporary file routines"
303.It "tmpfile	temporary file routines"
304.It "tmpnam	temporary file routines"
305.It "ungetc	un-get character from input stream"
306.It "ungetwc	un-get wide character from input stream"
307.It "vasprintf	formatted output conversion"
308.It "vfprintf	formatted output conversion"
309.It "vfscanf	input format conversion"
310.It "vprintf	formatted output conversion"
311.It "vscanf	input format conversion"
312.It "vsnprintf	formatted output conversion"
313.It "vsprintf	formatted output conversion"
314.It "vsscanf	input format conversion"
315.El
316