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.\" $FreeBSD$ 38.\" 39.Dd June 4, 1993 40.Dt FOPEN 3 41.Os 42.Sh NAME 43.Nm fopen , 44.Nm fdopen , 45.Nm freopen 46.Nd stream open functions 47.Sh LIBRARY 48.Lb libc 49.Sh SYNOPSIS 50.Fd #include <stdio.h> 51.Ft FILE * 52.Fn fopen "const char *path" "const char *mode" 53.Ft FILE * 54.Fn fdopen "int fildes" "const char *mode" 55.Ft FILE * 56.Fn freopen "const char *path" "const char *mode" "FILE *stream" 57.Sh DESCRIPTION 58The 59.Fn fopen 60function 61opens the file whose name is the string pointed to by 62.Fa path 63and associates a stream with it. 64.Pp 65The argument 66.Fa mode 67points to a string beginning with one of the following 68sequences (Additional characters may follow these sequences.): 69.Bl -tag -width indent 70.It Dq Li r 71Open text file for reading. 72The stream is positioned at the beginning of the file. 73.It Dq Li r+ 74Open for reading and writing. 75The stream is positioned at the beginning of the file. 76.It Dq Li w 77Truncate file to zero length or create text file for writing. 78The stream is positioned at the beginning of the file. 79.It Dq Li w+ 80Open for reading and writing. 81The file is created if it does not exist, otherwise it is truncated. 82The stream is positioned at the beginning of the file. 83.It Dq Li a 84Open for writing. 85The file is created if it does not exist. 86The stream is positioned at the end of the file. 87.It Dq Li a+ 88Open for reading and writing. 89The file is created if it does not exist. 90The stream is positioned at the end of the file. 91.El 92.Pp 93The 94.Fa mode 95string can also include the letter ``b'' either as a third character or 96as a character between the characters in any of the two-character strings 97described above. 98This is strictly for compatibility with 99.St -ansiC 100and has no effect; the ``b'' is ignored. 101.Pp 102Any created files will have mode 103.Pf \\*q Dv S_IRUSR 104\&| 105.Dv S_IWUSR 106\&| 107.Dv S_IRGRP 108\&| 109.Dv S_IWGRP 110\&| 111.Dv S_IROTH 112\&| 113.Dv S_IWOTH Ns \\*q 114.Pq Li 0666 , 115as modified by the process' 116umask value (see 117.Xr umask 2 ) . 118.Pp 119Reads and writes may be intermixed on read/write streams in any order, 120and do not require an intermediate seek as in previous versions of 121.Em stdio . 122This is not portable to other systems, however; 123.Tn ANSI C 124requires that 125a file positioning function intervene between output and input, unless 126an input operation encounters end-of-file. 127.Pp 128The 129.Fn fdopen 130function associates a stream with the existing file descriptor, 131.Fa fildes . 132The 133.Fa mode 134of the stream must be compatible with the mode of the file descriptor. 135When the stream is closed via 136.Xr fclose 3 , 137.Fa fildes 138is closed also. 139.Pp 140The 141.Fn freopen 142function 143opens the file whose name is the string pointed to by 144.Fa path 145and associates the stream pointed to by 146.Fa stream 147with it. 148The original stream (if it exists) is closed. 149The 150.Fa mode 151argument is used just as in the 152.Fn fopen 153function. 154The primary use of the 155.Fn freopen 156function 157is to change the file associated with a 158standard text stream 159.Pf ( Em stderr , 160.Em stdin , 161or 162.Em stdout ) . 163.Sh RETURN VALUES 164Upon successful completion 165.Fn fopen , 166.Fn fdopen 167and 168.Fn freopen 169return a 170.Tn FILE 171pointer. 172Otherwise, 173.Dv NULL 174is returned and the global variable 175.Va errno 176is set to indicate the error. 177.Sh ERRORS 178.Bl -tag -width [EINVAL] 179.It Bq Er EINVAL 180The 181.Fa mode 182provided to 183.Fn fopen , 184.Fn fdopen , 185or 186.Fn freopen 187was invalid. 188.El 189.Pp 190The 191.Fn fopen , 192.Fn fdopen 193and 194.Fn freopen 195functions 196may also fail and set 197.Va errno 198for any of the errors specified for the routine 199.Xr malloc 3 . 200.Pp 201The 202.Fn fopen 203function 204may also fail and set 205.Va errno 206for any of the errors specified for the routine 207.Xr open 2 . 208.Pp 209The 210.Fn fdopen 211function 212may also fail and set 213.Va errno 214for any of the errors specified for the routine 215.Xr fcntl 2 . 216.Pp 217The 218.Fn freopen 219function 220may also fail and set 221.Va errno 222for any of the errors specified for the routines 223.Xr open 2 , 224.Xr fclose 3 225and 226.Xr fflush 3 . 227.Sh SEE ALSO 228.Xr open 2 , 229.Xr fclose 3 , 230.Xr fseek 3 , 231.Xr funopen 3 232.Sh STANDARDS 233The 234.Fn fopen 235and 236.Fn freopen 237functions 238conform to 239.St -ansiC . 240The 241.Fn fdopen 242function 243conforms to 244.St -p1003.1-88 . 245