1*53821dcaSjmc.\" $OpenBSD: stdio.3,v 1.31 2014/07/03 06:08:06 jmc Exp $ 2df930be7Sderaadt.\" 3df930be7Sderaadt.\" Copyright (c) 1990, 1991, 1993 4df930be7Sderaadt.\" The Regents of the University of California. All rights reserved. 5df930be7Sderaadt.\" 6df930be7Sderaadt.\" Redistribution and use in source and binary forms, with or without 7df930be7Sderaadt.\" modification, are permitted provided that the following conditions 8df930be7Sderaadt.\" are met: 9df930be7Sderaadt.\" 1. Redistributions of source code must retain the above copyright 10df930be7Sderaadt.\" notice, this list of conditions and the following disclaimer. 11df930be7Sderaadt.\" 2. Redistributions in binary form must reproduce the above copyright 12df930be7Sderaadt.\" notice, this list of conditions and the following disclaimer in the 13df930be7Sderaadt.\" documentation and/or other materials provided with the distribution. 146580fee3Smillert.\" 3. Neither the name of the University nor the names of its contributors 15df930be7Sderaadt.\" may be used to endorse or promote products derived from this software 16df930be7Sderaadt.\" without specific prior written permission. 17df930be7Sderaadt.\" 18df930be7Sderaadt.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19df930be7Sderaadt.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20df930be7Sderaadt.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21df930be7Sderaadt.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22df930be7Sderaadt.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23df930be7Sderaadt.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24df930be7Sderaadt.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25df930be7Sderaadt.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26df930be7Sderaadt.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27df930be7Sderaadt.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28df930be7Sderaadt.\" SUCH DAMAGE. 29df930be7Sderaadt.\" 30*53821dcaSjmc.Dd $Mdocdate: July 3 2014 $ 31df930be7Sderaadt.Dt STDIO 3 32fc8533a3Saaron.Os 33df930be7Sderaadt.Sh NAME 34df930be7Sderaadt.Nm stdio 35df930be7Sderaadt.Nd standard input/output library functions 36df930be7Sderaadt.Sh SYNOPSIS 3764d4e987Stedu.In stdio.h 38aa86bf2dSaaron.Pp 39df930be7Sderaadt.Fd FILE *stdin; 40df930be7Sderaadt.Fd FILE *stdout; 41df930be7Sderaadt.Fd FILE *stderr; 42df930be7Sderaadt.Sh DESCRIPTION 43ef8b5cf9SjmcThe standard I/O library 44ef8b5cf9Sjmcprovides a simple and efficient buffered stream I/O interface. 45ef8b5cf9SjmcInput and output is mapped into logical data streams and the physical I/O 4610cc2884Saaroncharacteristics are concealed. 47aa86bf2dSaaronThe functions and macros are listed below; 48aa86bf2dSaaronmore information is available from the individual man pages. 49df930be7Sderaadt.Pp 50df930be7SderaadtA stream is associated with an external file (which may be a physical 51df930be7Sderaadtdevice) by 52aa86bf2dSaaron.Dq opening 5310cc2884Saarona file, which may involve creating a new file. 5410cc2884SaaronCreating an existing file causes its former contents to be discarded. 55df930be7SderaadtIf a file can support positioning requests (such as a disk file, as opposed 56df930be7Sderaadtto a terminal) then a 57aa86bf2dSaaron.Dq file position indicator 58df930be7Sderaadtassociated with the stream is positioned at the start of the file (byte 5910cc2884Saaronzero), unless the file is opened with append mode. 6010cc2884SaaronIf append mode 611eaaed80Sderaadtis used, the position indicator will be placed at the end-of-file. 622f2ed4fbSjaredyThe position indicator is maintained by subsequent reads, writes, 6310cc2884Saaronand positioning requests. 6410cc2884SaaronAll input occurs as if the characters 65df930be7Sderaadtwere read by successive calls to the 66df930be7Sderaadt.Xr fgetc 3 67df930be7Sderaadtfunction; all output takes place as if all characters were 681eaaed80Sderaadtwritten by successive calls to the 69df930be7Sderaadt.Xr fputc 3 70df930be7Sderaadtfunction. 71df930be7Sderaadt.Pp 72df930be7SderaadtA file is disassociated from a stream by 73aa86bf2dSaaron.Dq closing 74aa86bf2dSaaronit. 75df930be7SderaadtOutput streams are flushed (any unwritten buffer contents are transferred 76df930be7Sderaadtto the host environment) before the stream is disassociated from the file. 77df930be7SderaadtThe value of a pointer to a 78df930be7Sderaadt.Dv FILE 791eaaed80Sderaadtobject is indeterminate (garbage) after a file is closed. 80df930be7Sderaadt.Pp 81df930be7SderaadtA file may be subsequently reopened, by the same or another program 82df930be7Sderaadtexecution, and its contents reclaimed or modified (if it can be repositioned 8310cc2884Saaronat the start). 8410cc2884SaaronIf the main function returns to its original caller, or the 85df930be7Sderaadt.Xr exit 3 86df930be7Sderaadtfunction is called, all open files are closed (hence all output 8710cc2884Saaronstreams are flushed) before program termination. 8810cc2884SaaronOther methods of program termination may not close files properly and hence 8910cc2884Saaronbuffered output may be lost. 9010cc2884SaaronIn particular, 914371d1a5Sderaadt.Xr _exit 2 922f2ed4fbSjaredydoes not flush 932f2ed4fbSjaredy.Nm 942f2ed4fbSjaredyfiles. 9510cc2884SaaronNeither does an exit due to a signal. 964371d1a5SderaadtBuffers are flushed by 97436faa5fSjmc.Xr abort 3 , 98436faa5fSjmcas required by POSIX, although in previous implementations they were not. 99df930be7Sderaadt.Pp 100df930be7SderaadtThis implementation needs and makes 101df930be7Sderaadtno distinction between 102df930be7Sderaadt.Dq text 103df930be7Sderaadtand 104df930be7Sderaadt.Dq binary 105df930be7Sderaadtstreams. 106df930be7SderaadtIn effect, all streams are binary. 107df930be7SderaadtNo translation is performed and no extra padding appears on any stream. 108df930be7Sderaadt.Pp 109df930be7SderaadtAt program startup, three streams are predefined and need not be 110df930be7Sderaadtopened explicitly: 111aa86bf2dSaaron.Pp 112df930be7Sderaadt.Bl -bullet -compact -offset indent 113df930be7Sderaadt.It 114df930be7Sderaadt.Em standard input 115df930be7Sderaadt(for reading conventional input), 116df930be7Sderaadt.It 117df930be7Sderaadt.Em standard output 118df930be7Sderaadt(for writing conventional output), and 119df930be7Sderaadt.It 120df930be7Sderaadt.Em standard error 121df930be7Sderaadt(for writing diagnostic output). 122df930be7Sderaadt.El 123aa86bf2dSaaron.Pp 124df930be7SderaadtThese streams are abbreviated 125aa86bf2dSaaron.Em stdin , 126aa86bf2dSaaron.Em stdout , 127df930be7Sderaadtand 128df930be7Sderaadt.Em stderr . 129df930be7SderaadtInitially, the standard error stream 130df930be7Sderaadtis unbuffered; the standard input and output streams are 131df930be7Sderaadtfully buffered if and only if the streams do not refer to 132df930be7Sderaadtan interactive or 133df930be7Sderaadt.Dq terminal 134df930be7Sderaadtdevice, as determined by the 135df930be7Sderaadt.Xr isatty 3 136df930be7Sderaadtfunction. 137df930be7SderaadtIn fact, 138df930be7Sderaadt.Em all 139892a7bb8Saaronfreshly opened streams that refer to terminal devices 140df930be7Sderaadtdefault to line buffering, and 141df930be7Sderaadtpending output to such streams is written automatically 1421eaaed80Sderaadtwhenever such an input stream is read. 143df930be7SderaadtNote that this applies only to 144df930be7Sderaadt.Dq "true reads" ; 145df930be7Sderaadtif the read request can be satisfied by existing buffered data, 146df930be7Sderaadtno automatic flush will occur. 147df930be7SderaadtIn these cases, 148df930be7Sderaadtor when a large amount of computation is done after printing 149df930be7Sderaadtpart of a line on an output terminal, it is necessary to 150df930be7Sderaadt.Xr fflush 3 151*53821dcaSjmcthe standard output so that the output will appear immediately. 152df930be7SderaadtAlternatively, these defaults may be modified via the 153df930be7Sderaadt.Xr setvbuf 3 154df930be7Sderaadtfunction. 155df930be7Sderaadt.Pp 156df930be7SderaadtThe 157df930be7Sderaadt.Nm stdio 158ef8b5cf9Sjmclibrary is a part of the library libc 1596b714276Saaronand routines are automatically loaded as needed by the compiler. 16060addc80SjmcThe SYNOPSIS 161df930be7Sderaadtsections of the following manual pages indicate which include files 162df930be7Sderaadtare to be used, what the compiler declaration for the function 1632f2ed4fbSjaredylooks like, and which external variables are of interest. 164df930be7Sderaadt.Pp 165df930be7SderaadtThe following are defined as macros; 166df930be7Sderaadtthese names may not be re-used 167df930be7Sderaadtwithout first removing their current definitions with 168df930be7Sderaadt.Dv #undef : 169df930be7Sderaadt.Dv BUFSIZ , 170df930be7Sderaadt.Dv EOF , 171df930be7Sderaadt.Dv FILENAME_MAX , 172e613b67eSderaadt.Dv FOPEN_MAX , 173df930be7Sderaadt.Dv L_ctermid , 174df930be7Sderaadt.Dv L_tmpnam , 175df930be7Sderaadt.Dv NULL , 176df930be7Sderaadt.Dv SEEK_END , 177df930be7Sderaadt.Dv SEEK_SET , 1782f2ed4fbSjaredy.Dv SEEK_CUR , 179df930be7Sderaadt.Dv TMP_MAX , 180df930be7Sderaadt.Dv clearerr , 181df930be7Sderaadt.Dv feof , 182df930be7Sderaadt.Dv ferror , 183df930be7Sderaadt.Dv fileno , 184df930be7Sderaadt.Dv freopen , 185df930be7Sderaadt.Dv fwopen , 186df930be7Sderaadt.Dv getc , 187df930be7Sderaadt.Dv getchar , 188df930be7Sderaadt.Dv putc , 189df930be7Sderaadt.Dv putchar , 190df930be7Sderaadt.Dv stderr , 191df930be7Sderaadt.Dv stdin , 192df930be7Sderaadt.Dv stdout . 193df930be7SderaadtFunction versions of the macro functions 1942f2ed4fbSjaredy.Xr feof 3 , 1952f2ed4fbSjaredy.Xr ferror 3 , 1962f2ed4fbSjaredy.Xr clearerr 3 , 1972f2ed4fbSjaredy.Xr fileno 3 , 1982f2ed4fbSjaredy.Xr getc 3 , 1992f2ed4fbSjaredy.Xr getchar 3 , 2002f2ed4fbSjaredy.Xr putc 3 , 201df930be7Sderaadtand 2022f2ed4fbSjaredy.Xr putchar 3 2031eaaed80Sderaadtexist and will be used if the macro 204df930be7Sderaadtdefinitions are explicitly removed. 205df930be7Sderaadt.Sh LIST OF FUNCTIONS 206e324319bSjmc.Bl -column "sys_errlist" "Description" 207e324319bSjmc.It Sy Function Ta Sy Description 208e324319bSjmc.It asprintf Ta "formatted output conversion with allocation" 209e324319bSjmc.It clearerr Ta "check and reset stream status" 210f2c0f4acSbrad.It dprintf Ta "formatted output conversion" 211e324319bSjmc.It fclose Ta "close a stream" 212e324319bSjmc.It fdopen Ta "stream open functions" 213e324319bSjmc.It feof Ta "check and reset stream status" 214e324319bSjmc.It ferror Ta "check and reset stream status" 215e324319bSjmc.It fflush Ta "flush a stream" 216e324319bSjmc.It fgetc Ta "get next character or word from input stream" 217e324319bSjmc.It fgetln Ta "get a line from a stream" 218e324319bSjmc.It fgetpos Ta "reposition a stream" 219e324319bSjmc.It fgets Ta "get a line from a stream" 2200effee88Sbrad.It fgetwc Ta "get next wide character from input stream" 2210effee88Sbrad.It fgetws Ta "get a line of wide characters from a stream" 222e324319bSjmc.It fileno Ta "get a stream's underlying file descriptor" 223e324319bSjmc.It fopen Ta "stream open functions" 224e324319bSjmc.It fprintf Ta "formatted output conversion" 225e324319bSjmc.It fpurge Ta "flush a stream" 226e324319bSjmc.It fputc Ta "output a character or word to a stream" 227e324319bSjmc.It fputs Ta "output a line to a stream" 2280effee88Sbrad.It fputwc Ta "output a wide character to a stream" 2290effee88Sbrad.It fputws Ta "output a line of wide characters to a stream" 230e324319bSjmc.It fread Ta "binary stream input/output" 231e324319bSjmc.It freopen Ta "stream open functions" 232e324319bSjmc.It fropen Ta "open a stream" 233e324319bSjmc.It fscanf Ta "input format conversion" 234e324319bSjmc.It fseek Ta "reposition a stream" 235e324319bSjmc.It fsetpos Ta "reposition a stream" 236e324319bSjmc.It ftell Ta "reposition a stream" 237e324319bSjmc.It funopen Ta "open a stream" 2380effee88Sbrad.It fwide Ta "set/get orientation of stream" 239e324319bSjmc.It fwopen Ta "open a stream" 2400effee88Sbrad.It fwprintf Ta "formatted wide character output conversion" 241e324319bSjmc.It fwrite Ta "binary stream input/output" 242e324319bSjmc.It getc Ta "get next character or word from input stream" 243e324319bSjmc.It getchar Ta "get next character or word from input stream" 244436faa5fSjmc.It getdelim Ta "read a delimited record from a stream" 245436faa5fSjmc.It getline Ta "read a delimited record from a stream" 246e324319bSjmc.It getw Ta "get next character or word from input stream" 2470effee88Sbrad.It getwc Ta "get next wide character from input stream" 2480effee88Sbrad.It getwchar Ta "get next wide character from input stream" 2490effee88Sbrad.It mkdtemp Ta "create unique temporary directory" 250e324319bSjmc.It mkstemp Ta "create unique temporary file" 251e324319bSjmc.It mktemp Ta "create unique temporary file" 252e324319bSjmc.It perror Ta "system error messages" 253e324319bSjmc.It printf Ta "formatted output conversion" 254e324319bSjmc.It putc Ta "output a character or word to a stream" 255e324319bSjmc.It putchar Ta "output a character or word to a stream" 256e324319bSjmc.It puts Ta "output a line to a stream" 257e324319bSjmc.It putw Ta "output a character or word to a stream" 2580effee88Sbrad.It putwc Ta "output a wide character to a stream" 2590effee88Sbrad.It putwchar Ta "output a wide character to a stream" 260e324319bSjmc.It remove Ta "remove directory entry" 261e324319bSjmc.It rewind Ta "reposition a stream" 262e324319bSjmc.It scanf Ta "input format conversion" 263e324319bSjmc.It setbuf Ta "stream buffering operations" 264e324319bSjmc.It setbuffer Ta "stream buffering operations" 265e324319bSjmc.It setlinebuf Ta "stream buffering operations" 266e324319bSjmc.It setvbuf Ta "stream buffering operations" 267e324319bSjmc.It snprintf Ta "formatted output conversion" 268e324319bSjmc.It sprintf Ta "formatted output conversion" 269e324319bSjmc.It sscanf Ta "input format conversion" 270e324319bSjmc.It strerror Ta "system error messages" 2710effee88Sbrad.It swprintf Ta "formatted wide character output conversion" 272e324319bSjmc.It sys_errlist Ta "system error messages" 273e324319bSjmc.It sys_nerr Ta "system error messages" 274e324319bSjmc.It tempnam Ta "temporary file routines" 275e324319bSjmc.It tmpfile Ta "temporary file routines" 276e324319bSjmc.It tmpnam Ta "temporary file routines" 277e324319bSjmc.It ungetc Ta "un-get character from input stream" 2780effee88Sbrad.It ungetwc Ta "un-get wide character from input stream" 279e324319bSjmc.It vasprintf Ta "formatted output conversion with allocation" 280f2c0f4acSbrad.It vdprintf Ta "formatted output conversion" 281e324319bSjmc.It vfprintf Ta "formatted output conversion" 282e324319bSjmc.It vfscanf Ta "input format conversion" 2830effee88Sbrad.It vfwprintf Ta "formatted wide character output conversion" 284e324319bSjmc.It vprintf Ta "formatted output conversion" 285e324319bSjmc.It vscanf Ta "input format conversion" 286e324319bSjmc.It vsnprintf Ta "formatted output conversion" 287e324319bSjmc.It vsprintf Ta "formatted output conversion" 288e324319bSjmc.It vsscanf Ta "input format conversion" 2890effee88Sbrad.It vswprintf Ta "formatted wide character output conversion" 2900effee88Sbrad.It vwprintf Ta "formatted wide character output conversion" 2910effee88Sbrad.It wprintf Ta "formatted wide character output conversion" 292df930be7Sderaadt.El 293aa86bf2dSaaron.Sh SEE ALSO 294aa86bf2dSaaron.Xr close 2 , 295aa86bf2dSaaron.Xr open 2 , 296aa86bf2dSaaron.Xr read 2 , 297aa86bf2dSaaron.Xr write 2 298244c8eddSjmc.Sh STANDARDS 299244c8eddSjmcThe 300244c8eddSjmc.Nm stdio 301244c8eddSjmclibrary conforms to 302f98be9e3Sbrad.St -isoC-99 . 303aa86bf2dSaaron.Sh BUGS 304aa86bf2dSaaronThe standard buffered functions do not interact well with certain other 305aa86bf2dSaaronlibrary and system functions, especially 3062f2ed4fbSjaredy.Xr vfork 2 307aa86bf2dSaaronand 3082f2ed4fbSjaredy.Xr abort 3 . 309