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