xref: /csrg-svn/lib/libc/stdio/fopen.3 (revision 48352)
1*48352Scael.\" Copyright (c) 1990, 1991 The Regents of the University of California.
246078Sbostic.\" All rights reserved.
320391Smckusick.\"
446078Sbostic.\" This code is derived from software contributed to Berkeley by
546078Sbostic.\" Chris Torek.
646078Sbostic.\" %sccs.include.redist.man%
746078Sbostic.\"
8*48352Scael.\"     @(#)fopen.3	6.7 (Berkeley) 04/19/91
946078Sbostic.\"
10*48352Scael.Dd
11*48352Scael.Dt FOPEN 3
12*48352Scael.Os
13*48352Scael.Sh NAME
14*48352Scael.Nm fopen ,
15*48352Scael.Nm fdopen ,
16*48352Scael.Nm freopen
17*48352Scael.Nd stream open functions
18*48352Scael.Sh SYNOPSIS
19*48352Scael.Fd #include <stdio.h>
20*48352Scael.Ft FILE *
21*48352Scael.Fn fopen "char *path" "char *mode"
22*48352Scael.Ft FILE *
23*48352Scael.Fn fdopen "int fildes" "char *mode"
24*48352Scael.Ft FILE *
25*48352Scael.Fn freopen "char *path" "char *mode" "FILE *stream"
26*48352Scael.Sh DESCRIPTION
27*48352ScaelThe
28*48352Scael.Fn fopen
29*48352Scaelfunction
30*48352Scaelopens the file whose name is the string pointed to by
31*48352Scael.Fa path
32*48352Scaeland associates a stream with it.
33*48352Scael.Pp
34*48352ScaelThe argument
35*48352Scael.Fa mode
36*48352Scaelpoints to a string beginning with one of the following
37*48352Scaelsequences (Additional characters may follow these sequences.):
38*48352Scael.Bl -tag -width indent
39*48352Scael.It Dq Li r
40*48352ScaelOpen text file for reading.
4146078SbosticThe stream is positioned at the beginning of the file.
42*48352Scael.It Dq Li r+
4346078SbosticOpen for reading and writing.
4446078SbosticThe stream is positioned at the beginning of the file.
45*48352Scael.It Dq Li w
46*48352ScaelTruncate file to zero length or create text file for writing.
4746078SbosticThe stream is positioned at the beginning of the file.
48*48352Scael.No It Dq Li w+
4946078SbosticOpen for reading and writing.
5046078SbosticThe file is created if it does not exist, otherwise it is truncated.
5146078SbosticThe stream is positioned at the beginning of the file.
52*48352Scael.It Dq Li a
5346078SbosticOpen for writing.
5446078SbosticThe file is created if it does not exist.
5546078SbosticThe stream is positioned at the end of the file.
56*48352Scael.It Dq Li a+
5746078SbosticOpen for reading and writing.
5846078SbosticThe file is created if it does not exist.
5946078SbosticThe stream is positioned at the end of the file.
60*48352Scael.El
61*48352Scael.Pp
6246078SbosticThe
63*48352Scael.Fa mode
6446078Sbosticstring can also include the letter ``b'' either as a third character or
6546078Sbosticas a character between the characters in any of the two-character strings
6646078Sbosticdescribed above.
67*48352ScaelThis is strictly for compatibility with
68*48352Scael.St -ansiC
6946078Sbosticand has no effect; the ``b'' is ignored.
70*48352Scael.Pp
71*48352ScaelAny created files will have mode
72*48352Scael.Pf \\*q Dv S_IRUSR
73*48352Scael\&|
74*48352Scael.Dv S_IWUSR
75*48352Scael\&|
76*48352Scael.Dv S_IRGRP
77*48352Scael\&|
78*48352Scael.Dv S_IWGRP
79*48352Scael\&|
80*48352Scael.Dv S_IROTH
81*48352Scael\&|
82*48352Scael.Dv S_IWOTH Ns \\*q
83*48352Scael.Pq Li 0666 ,
84*48352Scaelas modified by the process'
8547010Sbosticumask value (see
86*48352Scael.Xr umask 2 ) .
87*48352Scael.Pp
8846078SbosticReads and writes may be intermixed on read/write streams in any order,
89*48352Scaeland do not require an intermediate seek as in previous versions of
90*48352Scael.Em stdio .
91*48352ScaelThis is not portable to other systems, however;
92*48352Scael.Tn ANSI C
93*48352Scaelrequires that
9446078Sbostica file positioning function intervene between output and input, unless
9546078Sbostican input operation encounters end-of-file.
96*48352Scael.Pp
9720391SmckusickThe
98*48352Scael.Fn fdopen
99*48352Scaelfunction associates a stream with the existing file descriptor,
100*48352Scael.Fa fildes .
101*48352ScaelThe
102*48352Scael.Fa mode
10346078Sbosticof the stream must be compatible with the mode of the file descriptor.
104*48352Scael.Pp
105*48352ScaelThe
106*48352Scael.Fn freopen
107*48352Scaelfunction
108*48352Scaelopens the file whose name is the string pointed to by
109*48352Scael.Fa path
110*48352Scaeland associates the stream pointed to by
111*48352Scael.Fa stream
112*48352Scaelwith it.
113*48352ScaelThe original stream (if it exists) is closed.
114*48352ScaelThe
115*48352Scael.Fa mode
116*48352Scaelargument is used just as in the
117*48352Scael.Xr fopen
118*48352Scaelfunction.
119*48352ScaelThe primary use of the
120*48352Scael.Fn freopen
121*48352Scaelfunction
122*48352Scaelis to change the file associated with a
123*48352Scaelstandard text stream
124*48352Scael.Pf ( Em stderr ,
125*48352Scael.Em stdin ,
126*48352Scaelor
127*48352Scael.Em stdout ) .
128*48352Scael.Sh RETURN VALUES
129*48352ScaelUpon successful completion
130*48352Scael.Fn fopen ,
131*48352Scael.Fn fdopen
13246078Sbosticand
133*48352Scael.Fn freopen
134*48352Scaelreturn a
135*48352Scael.Tn FILE
136*48352Scaelpointer.
137*48352ScaelOtherwise,
138*48352Scael.Dv NULL
139*48352Scaelis returned and the global variable
140*48352Scael.Va errno
14146078Sbosticis set to indicate the error.
142*48352Scael.Sh ERRORS
143*48352Scael.Bl -tag -width [EINVAL]
144*48352Scael.It Bq Er EINVAL
145*48352ScaelThe
146*48352Scael.Fa mode
147*48352Scaelprovided to
148*48352Scael.Fn fopen ,
149*48352Scael.Fn fdopen ,
15046078Sbosticor
151*48352Scael.Fn freopen
15246078Sbosticwas invalid.
153*48352Scael.El
154*48352Scael.Pp
155*48352ScaelThe
156*48352Scael.Fn fopen ,
157*48352Scael.Fn fdopen
15847010Sbosticand
159*48352Scael.Fn freopen
160*48352Scaelfunctions
16147010Sbosticmay also fail and set
162*48352Scael.Va errno
16347010Sbosticfor any of the errors specified for the routine
164*48352Scael.Xr malloc 3 .
165*48352Scael.Pp
166*48352ScaelThe
167*48352Scael.Fn fopen
168*48352Scaelfunction
16946078Sbosticmay also fail and set
170*48352Scael.Va errno
17146078Sbosticfor any of the errors specified for the routine
172*48352Scael.Xr open 2 .
173*48352Scael.Pp
174*48352ScaelThe
175*48352Scael.Fn fdopen
176*48352Scaelfunction
17746078Sbosticmay also fail and set
178*48352Scael.Va errno
17946078Sbosticfor any of the errors specified for the routine
180*48352Scael.Xr fcntl 2 .
181*48352Scael.Pp
182*48352ScaelThe
183*48352Scael.Fn freopen
184*48352Scaelfunction
18546078Sbosticmay also fail and set
186*48352Scael.Va errno
18746078Sbosticfor any of the errors specified for the routines
188*48352Scael.Xr open 2 ,
189*48352Scael.Xr fclose 3
19046078Sbosticand
191*48352Scael.Xr fflush 3 .
192*48352Scael.Sh SEE ALSO
193*48352Scael.Xr open 2 ,
194*48352Scael.Xr fclose 3 ,
195*48352Scael.Xr fseek 3 ,
196*48352Scael.Xr funopen 3
197*48352Scael.Sh STANDARDS
198*48352ScaelThe
199*48352Scael.Fn fopen
20046078Sbosticand
201*48352Scael.Fn freopen
202*48352Scaelfunctions
203*48352Scaelconform to
204*48352Scael.St -ansiC .
205*48352ScaelThe
206*48352Scael.Fn fdopen
207*48352Scaelfunction
208*48352Scaelconforms to
209*48352Scael.St -p1003.1-88 .
210