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 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 73read 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 after a file is closed (garbage). 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, such as 94.Xr abort 3 95do not bother about closing files properly. 96.Pp 97This implementation needs and makes 98no distinction between 99.Dq text 100and 101.Dq binary 102streams. 103In effect, all streams are binary. 104No translation is performed and no extra padding appears on any stream. 105.Pp 106At program startup, three streams are predefined and need not be 107opened explicitly: 108.Bl -bullet -compact -offset indent 109.It 110.Em standard input 111(for reading conventional input), 112.It 113.Em standard output 114(for writing conventional output), and 115.It 116.Em standard error 117(for writing diagnostic output). 118.El 119These streams are abbreviated 120.Em stdin , stdout 121and 122.Em stderr . 123Initially, the standard error stream 124is unbuffered; the standard input and output streams are 125fully buffered if and only if the streams do not refer to 126an interactive or 127.Dq terminal 128device, as determined by the 129.Xr isatty 3 130function. 131In fact, 132.Em all 133freshly-opened streams that refer to terminal devices 134default to line buffering, and 135pending output to such streams is written automatically 136whenever an such an input stream is read. 137Note that this applies only to 138.Dq "true reads" ; 139if the read request can be satisfied by existing buffered data, 140no automatic flush will occur. 141In these cases, 142or when a large amount of computation is done after printing 143part of a line on an output terminal, it is necessary to 144.Xr fflush 3 145the standard output before going off and computing so that the output 146will appear. 147Alternatively, these defaults may be modified via the 148.Xr setvbuf 3 149function. 150.Pp 151The 152.Nm stdio 153library is a part of the library 154.Nm libc 155and routines are automatically loaded as needed by the compilers 156.Xr cc 1 157and 158.Xr pc 1 . 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 SEEK_END , 178.Dv SEEK_SET , 179.Dv SEE_CUR , 180.Dv TMP_MAX , 181.Dv clearerr , 182.Dv feof , 183.Dv ferror , 184.Dv fileno , 185.Dv freopen , 186.Dv fwopen , 187.Dv getc , 188.Dv getchar , 189.Dv putc , 190.Dv putchar , 191.Dv stderr , 192.Dv stdin , 193.Dv stdout . 194Function versions of the macro functions 195.Fn feof , 196.Fn ferror , 197.Fn clearerr , 198.Fn fileno , 199.Fn getc , 200.Fn getchar , 201.Fn putc , 202and 203.Fn putchar 204exist and will be used if the macros 205definitions are explicitly removed. 206.Sh SEE ALSO 207.Xr open 2 , 208.Xr close 2 , 209.Xr read 2 , 210.Xr write 2 211.Sh BUGS 212The standard buffered functions do not interact well with certain other 213library and system functions, especially 214.Xr vfork 2 215and 216.Xr abort 3 . 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 225clearerr check and reset stream status 226fclose close a stream 227fdopen stream open functions 228feof check and reset stream status 229ferror check and reset stream status 230fflush flush a stream 231fgetc get next character or word from input stream 232fgetln get a line from a stream 233fgetpos reposition a stream 234fgets get a line from a stream 235fileno check and reset stream status 236fopen stream open functions 237fprintf formatted output conversion 238fpurge flush a stream 239fputc output a character or word to a stream 240fputs output a line to a stream 241fread binary stream input/output 242freopen stream open functions 243fropen open a stream 244fscanf input format conversion 245fseek reposition a stream 246fsetpos reposition a stream 247ftell reposition a stream 248funopen open a stream 249fwopen open a stream 250fwrite binary stream input/output 251getc get next character or word from input stream 252getchar get next character or word from input stream 253gets get a line from a stream 254getw get next character or word from input stream 255mkstemp create unique temporary file 256mktemp create unique temporary file 257perror system error messages 258printf formatted output conversion 259putc output a character or word to a stream 260putchar output a character or word to a stream 261puts output a line to a stream 262putw output a character or word to a stream 263remove remove directory entry 264rewind reposition a stream 265scanf input format conversion 266setbuf stream buffering operations 267setbuffer stream buffering operations 268setlinebuf stream buffering operations 269setvbuf stream buffering operations 270snprintf formatted output conversion 271sprintf formatted output conversion 272sscanf input format conversion 273strerror system error messages 274sys_errlist system error messages 275sys_nerr system error messages 276tempnam temporary file routines 277tmpfile temporary file routines 278tmpnam temporary file routines 279ungetc un-get character from input stream 280vfprintf formatted output conversion 281vfscanf input format conversion 282vprintf formatted output conversion 283vscanf input format conversion 284vsnprintf formatted output conversion 285vsprintf formatted output conversion 286vsscanf input format conversion 287.El 288