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