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