1.\" Copyright (c) 1990, 1991, 1993 2.\" The Regents of the University of California. All rights reserved. 3.\" 4.\" This code is derived from software contributed to Berkeley by 5.\" Chris Torek and the American National Standards Committee X3, 6.\" on Information Processing Systems. 7.\" 8.\" Redistribution and use in source and binary forms, with or without 9.\" modification, are permitted provided that the following conditions 10.\" are met: 11.\" 1. Redistributions of source code must retain the above copyright 12.\" notice, this list of conditions and the following disclaimer. 13.\" 2. Redistributions in binary form must reproduce the above copyright 14.\" notice, this list of conditions and the following disclaimer in the 15.\" documentation and/or other materials provided with the distribution. 16.\" 3. All advertising materials mentioning features or use of this software 17.\" must display the following acknowledgement: 18.\" This product includes software developed by the University of 19.\" California, Berkeley and its contributors. 20.\" 4. Neither the name of the University nor the names of its contributors 21.\" may be used to endorse or promote products derived from this software 22.\" without specific prior written permission. 23.\" 24.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34.\" SUCH DAMAGE. 35.\" 36.\" @(#)fopen.3 8.1 (Berkeley) 6/4/93 37.\" 38.Dd June 4, 1993 39.Dt FOPEN 3 40.Os 41.Sh NAME 42.Nm fopen , 43.Nm fdopen , 44.Nm freopen 45.Nd stream open functions 46.Sh SYNOPSIS 47.Fd #include <stdio.h> 48.Ft FILE * 49.Fn fopen "const char *path" "const char *mode" 50.Ft FILE * 51.Fn fdopen "int fildes" "const char *mode" 52.Ft FILE * 53.Fn freopen "const char *path" "const char *mode" "FILE *stream" 54.Sh DESCRIPTION 55The 56.Fn fopen 57function 58opens the file whose name is the string pointed to by 59.Fa path 60and associates a stream with it. 61.Pp 62The argument 63.Fa mode 64points to a string beginning with one of the following 65sequences (Additional characters may follow these sequences.): 66.Bl -tag -width indent 67.It Dq Li r 68Open text file for reading. 69The stream is positioned at the beginning of the file. 70.It Dq Li r+ 71Open for reading and writing. 72The stream is positioned at the beginning of the file. 73.It Dq Li w 74Truncate file to zero length or create text file for writing. 75The stream is positioned at the beginning of the file. 76.It Dq Li w+ 77Open for reading and writing. 78The file is created if it does not exist, otherwise it is truncated. 79The stream is positioned at the beginning of the file. 80.It Dq Li a 81Open for writing. 82The file is created if it does not exist. 83The stream is positioned at the end of the file. 84.It Dq Li a+ 85Open for reading and writing. 86The file is created if it does not exist. 87The stream is positioned at the end of the file. 88.El 89.Pp 90The 91.Fa mode 92string can also include the letter ``b'' either as a third character or 93as a character between the characters in any of the two-character strings 94described above. 95This is strictly for compatibility with 96.St -ansiC 97and has no effect; the ``b'' is ignored. 98.Pp 99Any created files will have mode 100.Pf \\*q Dv S_IRUSR 101\&| 102.Dv S_IWUSR 103\&| 104.Dv S_IRGRP 105\&| 106.Dv S_IWGRP 107\&| 108.Dv S_IROTH 109\&| 110.Dv S_IWOTH Ns \\*q 111.Pq Li 0666 , 112as modified by the process' 113umask value (see 114.Xr umask 2 ) . 115.Pp 116Reads and writes may be intermixed on read/write streams in any order, 117and do not require an intermediate seek as in previous versions of 118.Em stdio . 119This is not portable to other systems, however; 120.Tn ANSI C 121requires that 122a file positioning function intervene between output and input, unless 123an input operation encounters end-of-file. 124.Pp 125The 126.Fn fdopen 127function associates a stream with the existing file descriptor, 128.Fa fildes . 129The 130.Fa mode 131of the stream must be compatible with the mode of the file descriptor. 132When the stream is closed via 133.Xr fclose 3 , 134.Fa fildes 135is closed also. 136.Pp 137The 138.Fn freopen 139function 140opens the file whose name is the string pointed to by 141.Fa path 142and associates the stream pointed to by 143.Fa stream 144with it. 145The original stream (if it exists) is closed. 146The 147.Fa mode 148argument is used just as in the 149.Fn fopen 150function. 151The primary use of the 152.Fn freopen 153function 154is to change the file associated with a 155standard text stream 156.Pf ( Em stderr , 157.Em stdin , 158or 159.Em stdout ) . 160.Sh RETURN VALUES 161Upon successful completion 162.Fn fopen , 163.Fn fdopen 164and 165.Fn freopen 166return a 167.Tn FILE 168pointer. 169Otherwise, 170.Dv NULL 171is returned and the global variable 172.Va errno 173is set to indicate the error. 174.Sh ERRORS 175.Bl -tag -width [EINVAL] 176.It Bq Er EINVAL 177The 178.Fa mode 179provided to 180.Fn fopen , 181.Fn fdopen , 182or 183.Fn freopen 184was invalid. 185.El 186.Pp 187The 188.Fn fopen , 189.Fn fdopen 190and 191.Fn freopen 192functions 193may also fail and set 194.Va errno 195for any of the errors specified for the routine 196.Xr malloc 3 . 197.Pp 198The 199.Fn fopen 200function 201may also fail and set 202.Va errno 203for any of the errors specified for the routine 204.Xr open 2 . 205.Pp 206The 207.Fn fdopen 208function 209may also fail and set 210.Va errno 211for any of the errors specified for the routine 212.Xr fcntl 2 . 213.Pp 214The 215.Fn freopen 216function 217may also fail and set 218.Va errno 219for any of the errors specified for the routines 220.Xr open 2 , 221.Xr fclose 3 222and 223.Xr fflush 3 . 224.Sh SEE ALSO 225.Xr open 2 , 226.Xr fclose 3 , 227.Xr fseek 3 , 228.Xr funopen 3 229.Sh STANDARDS 230The 231.Fn fopen 232and 233.Fn freopen 234functions 235conform to 236.St -ansiC . 237The 238.Fn fdopen 239function 240conforms to 241.St -p1003.1-88 . 242