1.\" $NetBSD: fopen.3,v 1.22 2003/08/07 16:43:24 agc Exp $ 2.\" 3.\" Copyright (c) 1990, 1991, 1993 4.\" The Regents of the University of California. All rights reserved. 5.\" 6.\" This code is derived from software contributed to Berkeley by 7.\" Chris Torek and the American National Standards Committee X3, 8.\" on Information Processing Systems. 9.\" 10.\" Redistribution and use in source and binary forms, with or without 11.\" modification, are permitted provided that the following conditions 12.\" are met: 13.\" 1. Redistributions of source code must retain the above copyright 14.\" notice, this list of conditions and the following disclaimer. 15.\" 2. Redistributions in binary form must reproduce the above copyright 16.\" notice, this list of conditions and the following disclaimer in the 17.\" documentation and/or other materials provided with the distribution. 18.\" 3. Neither the name of the University nor the names of its contributors 19.\" may be used to endorse or promote products derived from this software 20.\" without specific prior written permission. 21.\" 22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32.\" SUCH DAMAGE. 33.\" 34.\" @(#)fopen.3 8.1 (Berkeley) 6/4/93 35.\" 36.Dd June 4, 1993 37.Dt FOPEN 3 38.Os 39.Sh NAME 40.Nm fopen , 41.Nm fdopen , 42.Nm freopen 43.Nd stream open functions 44.Sh LIBRARY 45.Lb libc 46.Sh SYNOPSIS 47.In 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 * restrict 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 for reading. 69.It Dq Li r+ 70Open for reading and writing. 71.It Dq Li w 72Open for writing. 73Truncate file to zero length or create file. 74.It Dq Li w+ 75Open for reading and writing. 76Truncate file to zero length or create file. 77.It Dq Li a 78Append; open for writing. 79The file is created if it does not exist. 80.It Dq Li a+ 81Append; open for reading and writing. 82The file is created if it does not exist. 83.El 84.Pp 85The 86.Fa mode 87string can also include the letter ``b'' either as a last character or 88as a character between the characters in any of the two-character strings 89described above. 90This is strictly for compatibility with 91.St -ansiC 92and has no effect; the ``b'' is ignored. 93.Pp 94The letter ``f'' in the mode string restricts fopen to regular 95files; if the file opened is not a regular file, 96.Fn fopen 97will fail. 98This is a non 99.St -ansiC 100extension. 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 119Opening a file with append mode causes all subsequent writes to it 120to be forced to the then current end of file, regardless of intervening 121repositioning of the stream. 122.Pp 123The 124.Fn fopen 125and 126.Fn freopen 127functions initially position the stream at the start of the file 128unless the file is opened with append mode, 129in which case the stream is initially positioned at the end of the file. 130.\" PR 6072 claims this paragraph is not correct. 131.\" .Pp 132.\" Reads and writes may be intermixed on read/write streams in any order, 133.\" and do not require an intermediate seek as in previous versions of 134.\" .Em stdio . 135.\" This is not portable to other systems, however; 136.\" .Tn ANSI C 137.\" requires that 138.\" a file positioning function intervene between output and input, unless 139.\" an input operation encounters end-of-file. 140.Pp 141The 142.Fn fdopen 143function associates a stream with the existing file descriptor, 144.Fa fildes . 145The 146.Fa mode 147of the stream must be compatible with the mode of the file descriptor. 148The stream is positioned at the file offset of the file descriptor. 149.Pp 150The 151.Fn freopen 152function 153opens the file whose name is the string pointed to by 154.Fa path 155and associates the stream pointed to by 156.Fa stream 157with it. 158The original stream (if it exists) is closed. 159The 160.Fa mode 161argument is used just as in the 162.Fn fopen 163function. 164The primary use of the 165.Fn freopen 166function 167is to change the file associated with a 168standard text stream 169.Pf ( Em stderr , 170.Em stdin , 171or 172.Em stdout ) . 173.Sh RETURN VALUES 174Upon successful completion 175.Fn fopen , 176.Fn fdopen 177and 178.Fn freopen 179return a 180.Tn FILE 181pointer. 182Otherwise, 183.Dv NULL 184is returned and the global variable 185.Va errno 186is set to indicate the error. 187.Sh ERRORS 188.Bl -tag -width Er 189.It Bq Er EINVAL 190The 191.Fa mode 192provided to 193.Fn fopen , 194.Fn fdopen , 195or 196.Fn freopen 197was invalid. 198.It Bq Er EFTYPE 199The file is not a regular file and the character ``f'' is specified 200in the mode. 201.El 202.Pp 203The 204.Fn fopen , 205.Fn fdopen 206and 207.Fn freopen 208functions 209may also fail and set 210.Va errno 211for any of the errors specified for the routine 212.Xr malloc 3 . 213.Pp 214The 215.Fn fopen 216function 217may also fail and set 218.Va errno 219for any of the errors specified for the routine 220.Xr open 2 . 221.Pp 222The 223.Fn fdopen 224function 225may also fail and set 226.Va errno 227for any of the errors specified for the routine 228.Xr fcntl 2 . 229.Pp 230The 231.Fn freopen 232function 233may also fail and set 234.Va errno 235for any of the errors specified for the routines 236.Xr open 2 , 237.Xr fclose 3 238and 239.Xr fflush 3 . 240.Sh SEE ALSO 241.Xr open 2 , 242.Xr fclose 3 , 243.Xr fileno 3 , 244.Xr fseek 3 , 245.Xr funopen 3 246.Sh STANDARDS 247The 248.Fn fopen 249and 250.Fn freopen 251functions 252conform to 253.St -ansiC . 254The 255.Fn fdopen 256function conforms to 257.St -p1003.1-90 . 258.Sh CAVEATS 259Proper code using 260.Fn fdopen 261with error checking should 262.Xr close 2 263.Fa fildes 264in case of failure, and 265.Xr fclose 3 266the resulting FILE * in case of success. 267.Bd -literal 268 FILE *file; 269 int fd; 270 271 if ((file = fdopen(fd, "r")) != NULL) { 272 /* perform operations on the FILE * */ 273 fclose(file); 274 } else { 275 /* failure, report the error */ 276 close(fd); 277 } 278.Ed 279