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 April 19, 1994 36.Dt STDIO 3 37.Os BSD 4 38.Sh NAME 39.Nm stdio 40.Nd standard input/output library functions 41.Sh SYNOPSIS 42.Fd #include <stdio.h> 43.Fd FILE *stdin; 44.Fd FILE *stdout; 45.Fd FILE *stderr; 46.Sh DESCRIPTION 47The standard 48.Tn I/O 49library provides a simple and efficient buffered stream 50.Tn I/O 51interface. 52Input and output is mapped into logical data streams 53and the physical 54.Tn I/O 55characteristics are concealed. 56The functions and macros are listed 57below; more information is available from the individual man pages. 58.Pp 59A stream is associated with an external file (which may be a physical 60device) by 61.Em opening 62a file, which may involve creating a new file. 63Creating an 64existing file causes its former contents to be discarded. 65If a file can support positioning requests (such as a disk file, as opposed 66to a terminal) then a 67.Em file position indicator 68associated with the stream is positioned at the start of the file (byte 69zero), unless the file is opened with append mode. 70If append mode 71is used, the position indicator will be placed at the end-of-file. 72The position indicator is maintained by subsequent reads, writes 73and positioning requests. 74All input occurs as if the characters 75were read by successive calls to the 76.Xr fgetc 3 77function; all output takes place as if all characters were 78written by successive calls to the 79.Xr fputc 3 80function. 81.Pp 82A file is disassociated from a stream by 83.Em closing 84the file. 85Output streams are flushed (any unwritten buffer contents are transferred 86to the host environment) before the stream is disassociated from the file. 87The value of a pointer to a 88.Dv FILE 89object is indeterminate (garbage) after a file is closed. 90.Pp 91A file may be subsequently reopened, by the same or another program 92execution, and its contents reclaimed or modified (if it can be repositioned 93at the start). If the main function returns to its original caller, or 94the 95.Xr exit 3 96function is called, all open files are closed (hence all output 97streams are flushed) before program termination. Other methods 98of program termination may not close files properly and hence 99buffered output may be lost. In particular, 100.Xr _exit 2 101does not flush stdio files. Neither does an exit due to a signal. 102Buffers are flushed by 103.Xr abort 3 104as required by POSIX, although previous implementations did not. 105.Pp 106This implementation makes no distinction between 107.Dq text 108and 109.Dq binary 110streams. 111In effect, all streams are binary. 112No translation is performed and no extra padding appears on any stream. 113.Pp 114At program startup, three streams are predefined and need not be 115opened explicitly: 116.Bl -bullet -compact -offset indent 117.It 118.Em standard input 119(for reading conventional input), 120.It 121.Em standard output 122(for writing conventional output), and 123.It 124.Em standard error 125(for writing diagnostic output). 126.El 127These streams are abbreviated 128.Em stdin , stdout 129and 130.Em stderr . 131Initially, the standard error stream 132is unbuffered; the standard input and output streams are 133fully buffered if and only if the streams do not refer to 134an interactive or 135.Dq terminal 136device, as determined by the 137.Xr isatty 3 138function. 139In fact, 140.Em all 141freshly-opened streams that refer to terminal devices 142default to line buffering, and 143pending output to such streams is written automatically 144whenever such an input stream is read. 145Note that this applies only to 146.Dq "true reads" ; 147if the read request can be satisfied by existing buffered data, 148no automatic flush will occur. 149In these cases, 150or when a large amount of computation is done after printing 151part of a line on an output terminal, it is necessary to 152.Xr fflush 3 153the standard output before going off and computing so that the output 154will appear. 155Alternatively, these defaults may be modified via the 156.Xr setvbuf 3 157function. 158.Pp 159The 160.Nm stdio 161library is a part of the library 162.Nm libc 163and routines are automatically loaded as needed by the C compiler. 164The 165.Tn SYNOPSIS 166sections of the following manual pages indicate which include files 167are to be used, what the compiler declaration for the function 168looks like and which external variables are of interest. 169.Pp 170The following are defined as macros; 171these names may not be re-used 172without first removing their current definitions with 173.Dv #undef : 174.Dv BUFSIZ , 175.Dv EOF , 176.Dv FILENAME_MAX , 177.Dv FOPEN_MAX , 178.Dv L_cuserid , 179.Dv L_ctermid , 180.Dv L_tmpnam, 181.Dv NULL , 182.Dv P_tmpdir, 183.Dv SEEK_CUR , 184.Dv SEEK_END , 185.Dv SEEK_SET , 186.Dv TMP_MAX , 187.Dv clearerr , 188.Dv feof , 189.Dv ferror , 190.Dv fileno , 191.Dv fropen , 192.Dv fwopen , 193.Dv getc , 194.Dv getchar , 195.Dv putc , 196.Dv putchar , 197.Dv stderr , 198.Dv stdin , 199.Dv stdout , 200.Dv vfscanf . 201Function versions of the macro functions 202.Fn clearerr , 203.Fn feof , 204.Fn ferror , 205.Fn fileno , 206.Fn getc , 207.Fn getchar , 208.Fn putc , 209and 210.Fn putchar 211exist and will be used if the macro 212definitions are explicitly removed. 213.Sh SEE ALSO 214.Xr close 2 , 215.Xr open 2 , 216.Xr read 2 , 217.Xr write 2 218.Sh BUGS 219The standard buffered functions do not interact well with certain other 220library and system functions, especially 221.Xr vfork 2 . 222.Sh STANDARDS 223The 224.Nm stdio 225library conforms to 226.St -ansiC . 227.Sh LIST OF FUNCTIONS 228.Bl -column "Description" 229.Sy Function Description 230asprintf formatted output conversion 231clearerr check and reset stream status 232fclose close a stream 233fdopen stream open functions 234feof check and reset stream status 235ferror check and reset stream status 236fflush flush a stream 237fgetc get next character or word from input stream 238fgetln get a line from a stream 239fgetpos reposition a stream 240fgets get a line from a stream 241fileno check and reset stream status 242fopen stream open functions 243fprintf formatted output conversion 244fpurge flush a stream 245fputc output a character or word to a stream 246fputs output a line to a stream 247fread binary stream input/output 248freopen stream open functions 249fropen open a stream 250fscanf input format conversion 251fseek reposition a stream 252fsetpos reposition a stream 253ftell reposition a stream 254funopen open a stream 255fwopen open a stream 256fwrite binary stream input/output 257getc get next character or word from input stream 258getchar get next character or word from input stream 259gets get a line from a stream 260getw get next character or word from input stream 261mkdtemp create unique temporary file 262mkstemp create unique temporary file 263mktemp create unique temporary file 264perror system error messages 265printf formatted output conversion 266putc output a character or word to a stream 267putchar output a character or word to a stream 268puts output a line to a stream 269putw output a character or word to a stream 270remove remove directory entry 271rewind reposition a stream 272scanf input format conversion 273setbuf stream buffering operations 274setbuffer stream buffering operations 275setlinebuf stream buffering operations 276setvbuf stream buffering operations 277snprintf formatted output conversion 278sprintf formatted output conversion 279sscanf input format conversion 280strerror system error messages 281sys_errlist system error messages 282sys_nerr system error messages 283tempnam temporary file routines 284tmpfile temporary file routines 285tmpnam temporary file routines 286ungetc un-get character from input stream 287vasprintf formatted output conversion 288vfprintf formatted output conversion 289vfscanf input format conversion 290vprintf formatted output conversion 291vscanf input format conversion 292vsnprintf formatted output conversion 293vsprintf formatted output conversion 294vsscanf input format conversion 295.El 296