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. Neither the name of the University nor the names of its contributors 13.\" may be used to endorse or promote products derived from this software 14.\" without specific prior written permission. 15.\" 16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26.\" SUCH DAMAGE. 27.\" 28.\" @(#)stdio.3 8.7 (Berkeley) 4/19/94 29.\" $FreeBSD$ 30.\" 31.Dd March 3, 2009 32.Dt STDIO 3 33.Os 34.Sh NAME 35.Nm stdio 36.Nd standard input/output library functions 37.Sh LIBRARY 38.Lb libc 39.Sh SYNOPSIS 40.In stdio.h 41.Vt FILE *stdin ; 42.Vt FILE *stdout ; 43.Vt FILE *stderr ; 44.Sh DESCRIPTION 45The standard 46.Tn I/O 47library provides a simple and efficient buffered stream 48.Tn I/O 49interface. 50Input and output is mapped into logical data streams 51and the physical 52.Tn I/O 53characteristics are concealed. 54The 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. 61Creating an 62existing file causes its former contents to be discarded. 63If a file can support positioning requests (such as a disk file, as opposed 64to a terminal) then a 65.Em file position indicator 66associated with the stream is positioned at the start of the file (byte 67zero), unless the file is opened with append mode. 68If append mode 69is used, the position indicator will be placed at the end-of-file. 70The position indicator is maintained by subsequent reads, writes 71and positioning requests. 72All input occurs as if the characters 73were read by successive calls to the 74.Xr fgetc 3 75function; all output takes place as if all characters were 76written by successive calls to the 77.Xr fputc 3 78function. 79.Pp 80A file is disassociated from a stream by 81.Em closing 82the file. 83Output streams are flushed (any unwritten buffer contents are transferred 84to the host environment) before the stream is disassociated from the file. 85The value of a pointer to a 86.Dv FILE 87object is indeterminate (garbage) after a file is closed. 88.Pp 89A file may be subsequently reopened, by the same or another program 90execution, and its contents reclaimed or modified (if it can be repositioned 91at the start). 92If the main function returns to its original caller, or 93the 94.Xr exit 3 95function is called, all open files are closed (hence all output 96streams are flushed) before program termination. 97Other methods 98of program termination may not close files properly and hence 99buffered output may be lost. 100In particular, 101.Xr _exit 2 102does not flush stdio files. 103Neither 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.Dv stdin , stdout 131and 132.Dv 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.Ic #undef : 176.Dv BUFSIZ , 177.Dv EOF , 178.Dv FILENAME_MAX , 179.Dv FOPEN_MAX , 180.Dv L_ctermid , 181.Dv L_cuserid , 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 clearerr_unlocked , 191.Dv feof , 192.Dv feof_unlocked , 193.Dv ferror , 194.Dv ferror_unlocked , 195.Dv fileno , 196.Dv fileno_unlocked , 197.Dv fropen , 198.Dv fwopen , 199.Dv getc , 200.Dv getc_unlocked , 201.Dv getchar , 202.Dv getchar_unlocked , 203.Dv putc , 204.Dv putc_unlocked , 205.Dv putchar , 206.Dv putchar_unlocked , 207.Dv stderr , 208.Dv stdin 209and 210.Dv stdout . 211Function versions of the macro functions 212.Dv clearerr , 213.Dv clearerr_unlocked , 214.Dv feof , 215.Dv feof_unlocked , 216.Dv ferror , 217.Dv ferror_unlocked , 218.Dv fileno , 219.Dv fileno_unlocked , 220.Dv getc , 221.Dv getc_unlocked , 222.Dv getchar , 223.Dv getchar_unlocked , 224.Dv putc , 225.Dv putc_unlocked , 226.Dv putchar , 227and 228.Dv putchar_unlocked 229exist and will be used if the macro 230definitions are explicitly removed. 231.Sh SEE ALSO 232.Xr close 2 , 233.Xr open 2 , 234.Xr read 2 , 235.Xr write 2 236.Sh STANDARDS 237The 238.Nm 239library conforms to 240.St -isoC-99 . 241.Sh LIST OF FUNCTIONS 242.Bl -column "Description" 243.It Sy "Function Description" 244.It "asprintf formatted output conversion" 245.It "clearerr check and reset stream status" 246.It "dprintf formatted output conversion" 247.It "fclose close a stream" 248.It "fdopen stream open functions" 249.It "feof check and reset stream status" 250.It "ferror check and reset stream status" 251.It "fflush flush a stream" 252.It "fgetc get next character or word from input stream" 253.It "fgetln get a line from a stream" 254.It "fgetpos reposition a stream" 255.It "fgets get a line from a stream" 256.It "fgetwc get next wide character from input stream" 257.It "fgetws get a line of wide characters from a stream" 258.It "fileno check and reset stream status" 259.It "fopen stream open functions" 260.It "fprintf formatted output conversion" 261.It "fpurge flush a stream" 262.It "fputc output a character or word to a stream" 263.It "fputs output a line to a stream" 264.It "fputwc output a wide character to a stream" 265.It "fputws output a line of wide characters to a stream" 266.It "fread binary stream input/output" 267.It "freopen stream open functions" 268.It "fropen open a stream" 269.It "fscanf input format conversion" 270.It "fseek reposition a stream" 271.It "fsetpos reposition a stream" 272.It "ftell reposition a stream" 273.It "funopen open a stream" 274.It "fwide set/get orientation of stream" 275.It "fwopen open a stream" 276.It "fwprintf formatted wide character output conversion" 277.It "fwrite binary stream input/output" 278.It "getc get next character or word from input stream" 279.It "getchar get next character or word from input stream" 280.It "getdelim get a line from a stream" 281.It "getline get a line from a stream" 282.It "gets get a line from a stream" 283.It "getw get next character or word from input stream" 284.It "getwc get next wide character from input stream" 285.It "getwchar get next wide character from input stream" 286.It "mkdtemp create unique temporary directory" 287.It "mkstemp create unique temporary file" 288.It "mktemp create unique temporary file" 289.It "perror system error messages" 290.It "printf formatted output conversion" 291.It "putc output a character or word to a stream" 292.It "putchar output a character or word to a stream" 293.It "puts output a line to a stream" 294.It "putw output a character or word to a stream" 295.It "putwc output a wide character to a stream" 296.It "putwchar output a wide character to a stream" 297.It "remove remove directory entry" 298.It "rewind reposition a stream" 299.It "scanf input format conversion" 300.It "setbuf stream buffering operations" 301.It "setbuffer stream buffering operations" 302.It "setlinebuf stream buffering operations" 303.It "setvbuf stream buffering operations" 304.It "snprintf formatted output conversion" 305.It "sprintf formatted output conversion" 306.It "sscanf input format conversion" 307.It "strerror system error messages" 308.It "swprintf formatted wide character output conversion" 309.It "sys_errlist system error messages" 310.It "sys_nerr system error messages" 311.It "tempnam temporary file routines" 312.It "tmpfile temporary file routines" 313.It "tmpnam temporary file routines" 314.It "ungetc un-get character from input stream" 315.It "ungetwc un-get wide character from input stream" 316.It "vasprintf formatted output conversion" 317.It "vdprintf formatted output conversion" 318.It "vfprintf formatted output conversion" 319.It "vfscanf input format conversion" 320.It "vfwprintf formatted wide character output conversion" 321.It "vprintf formatted output conversion" 322.It "vscanf input format conversion" 323.It "vsnprintf formatted output conversion" 324.It "vsprintf formatted output conversion" 325.It "vsscanf input format conversion" 326.It "vswprintf formatted wide character output conversion" 327.It "vwprintf formatted wide character output conversion" 328.It "wprintf formatted wide character output conversion" 329.El 330.Sh BUGS 331The standard buffered functions do not interact well with certain other 332library and system functions, especially 333.Xr vfork 2 . 334