1.\" $NetBSD: fopen.3,v 1.27 2011/07/18 05:17:16 jruoho 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 July 18, 2011 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 * restrict path" "const char * restrict mode" 50.Ft FILE * 51.Fn fdopen "int fildes" "const char *mode" 52.Ft FILE * 53.Fn freopen "const char * restrict path" "const char * restrict 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 85Additionally: 86.Bl -bullet -offset 2n 87.It 88The 89.Fa mode 90string can also include the letter 91.Dq b 92either as a last 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 98.Dq b 99is ignored. 100.It 101The letter 102.Dq f 103in the mode string restricts 104.Fn fopen 105to regular files; if the file opened is not a regular file, 106.Fn fopen 107will fail. 108This is a non 109.St -ansiC 110extension. 111.It 112The letter 113.Dq e 114in the mode string sets the close-on-exec flag in the file descriptors of 115the newly opened file files; if the operation fails, 116.Fn fopen 117will fail. 118This is a non 119.St -ansiC 120extension. 121.El 122.Pp 123Any created files will have mode 124.Pf \*q Dv S_IRUSR 125\&| 126.Dv S_IWUSR 127\&| 128.Dv S_IRGRP 129\&| 130.Dv S_IWGRP 131\&| 132.Dv S_IROTH 133\&| 134.Dv S_IWOTH Ns \*q 135.Pq Li 0666 , 136as modified by the process' 137.Xr umask 2 138value. 139.Pp 140Opening a file with append mode causes all subsequent writes to it 141to be forced to the then current end of file, regardless of intervening 142repositioning of the stream. 143.Pp 144The 145.Fn fopen 146and 147.Fn freopen 148functions initially position the stream at the start of the file 149unless the file is opened with append mode, 150in which case the stream is initially positioned at the end of the file. 151.\" PR 6072 claims this paragraph is not correct. 152.\" .Pp 153.\" Reads and writes may be intermixed on read/write streams in any order, 154.\" and do not require an intermediate seek as in previous versions of 155.\" .Em stdio . 156.\" This is not portable to other systems, however; 157.\" .Tn ANSI C 158.\" requires that 159.\" a file positioning function intervene between output and input, unless 160.\" an input operation encounters end-of-file. 161.Pp 162The 163.Fn fdopen 164function associates a stream with the existing file descriptor, 165.Fa fildes . 166The 167.Fa mode 168of the stream must be compatible with the mode of the file descriptor. 169The stream is positioned at the file offset of the file descriptor. 170.Pp 171The 172.Fn freopen 173function 174opens the file whose name is the string pointed to by 175.Fa path 176and associates the stream pointed to by 177.Fa stream 178with it. 179The original stream (if it exists) is closed. 180The 181.Fa mode 182argument is used just as in the 183.Fn fopen 184function. 185The primary use of the 186.Fn freopen 187function 188is to change the file associated with a 189standard text stream 190.Pf ( Em stderr , 191.Em stdin , 192or 193.Em stdout ) . 194.Sh RETURN VALUES 195Upon successful completion 196.Fn fopen , 197.Fn fdopen 198and 199.Fn freopen 200return a 201.Tn FILE 202pointer. 203Otherwise, 204.Dv NULL 205is returned and the global variable 206.Va errno 207is set to indicate the error. 208.Sh ERRORS 209The functions may fail if: 210.Bl -tag -width Er 211.It Bq Er EFTYPE 212The file is not a regular file and the character ``f'' is specified 213in the mode. 214.It Bq Er EINVAL 215The specified 216.Fa mode 217was invalid. 218.El 219.Pp 220The 221.Fn fopen , 222.Fn fdopen 223and 224.Fn freopen 225functions 226may also fail and set 227.Va errno 228for any of the errors specified for the routine 229.Xr malloc 3 . 230.Pp 231The 232.Fn fopen 233function 234may also fail and set 235.Va errno 236for any of the errors specified for the routine 237.Xr open 2 . 238.Pp 239The 240.Fn fdopen 241function 242may also fail and set 243.Va errno 244for any of the errors specified for the routine 245.Xr fcntl 2 . 246.Pp 247The 248.Fn freopen 249function 250may also fail and set 251.Va errno 252for any of the errors specified for the routines 253.Xr open 2 , 254.Xr fclose 3 255and 256.Xr fflush 3 . 257.Sh SEE ALSO 258.Xr open 2 , 259.Xr fclose 3 , 260.Xr fileno 3 , 261.Xr fseek 3 , 262.Xr funopen 3 263.Sh STANDARDS 264The 265.Fn fopen 266and 267.Fn freopen 268functions conform to 269.St -ansiC . 270All three functions are specified in 271.St -p1003.1-2008 . 272.Sh CAVEATS 273Proper code using 274.Fn fdopen 275with error checking should 276.Xr close 2 277.Fa fildes 278in case of failure, and 279.Xr fclose 3 280the resulting FILE * in case of success. 281.Bd -literal 282 FILE *file; 283 int fd; 284 285 if ((file = fdopen(fd, "r")) != NULL) { 286 /* perform operations on the FILE * */ 287 fclose(file); 288 } else { 289 /* failure, report the error */ 290 close(fd); 291 } 292.Ed 293