]>
Commit | Line | Data |
---|---|---|
fea681da MK |
1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. |
2 | .\" 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 6.8 (Berkeley) 6/29/91 | |
37 | .\" | |
38 | .\" Converted for Linux, Mon Nov 29 15:22:01 1993, faith@cs.unc.edu | |
39 | .\" Modified, aeb, 960421, 970806 | |
40 | .\" Modified, joey, aeb, 2002-01-03 | |
41 | .\" | |
69962544 | 42 | .TH FOPEN 3 2006-05-04 "GNU" "Linux Programmer's Manual" |
fea681da MK |
43 | .SH NAME |
44 | fopen, fdopen, freopen \- stream open functions | |
45 | .SH SYNOPSIS | |
46 | .B #include <stdio.h> | |
47 | .sp | |
48 | .BI "FILE *fopen(const char *" path ", const char *" mode ); | |
49 | .br | |
50 | .BI "FILE *fdopen(int " fildes ", const char *" mode ); | |
51 | .br | |
52 | .BI "FILE *freopen(const char *" path ", const char *" mode ", FILE *" stream ); | |
53 | .SH DESCRIPTION | |
54 | The | |
e511ffb6 | 55 | .BR fopen () |
fea681da MK |
56 | function opens the file whose name is the string pointed to by |
57 | .I path | |
58 | and associates a stream with it. | |
59 | .PP | |
60 | The argument | |
61 | .I mode | |
62 | points to a string beginning with one of the following sequences | |
63 | (Additional characters may follow these sequences.): | |
64 | .TP | |
65 | .B r | |
c13182ef MK |
66 | Open text file for reading. |
67 | The stream is positioned at the beginning of the file. | |
fea681da MK |
68 | .TP |
69 | .B r+ | |
c13182ef MK |
70 | Open for reading and writing. |
71 | The stream is positioned at the beginning of the file. | |
fea681da MK |
72 | .TP |
73 | .B w | |
c13182ef MK |
74 | Truncate file to zero length or create text file for writing. |
75 | The stream is positioned at the beginning of the file. | |
fea681da MK |
76 | .TP |
77 | .B w+ | |
c13182ef MK |
78 | Open for reading and writing. |
79 | The file is created if it does not exist, otherwise it is truncated. | |
80 | The stream is positioned at the beginning of | |
fea681da MK |
81 | the file. |
82 | .TP | |
83 | .B a | |
c13182ef MK |
84 | Open for appending (writing at end of file). |
85 | The file is created if it does not exist. | |
86 | The stream is positioned at the end of the file. | |
fea681da MK |
87 | .TP |
88 | .B a+ | |
c13182ef MK |
89 | Open for reading and appending (writing at end of file). |
90 | The file is created if it does not exist. | |
be9dccdf MK |
91 | The initial file position for reading is at the beginning of the file, |
92 | but output is always appended to the end of the file. | |
fea681da MK |
93 | .PP |
94 | The | |
95 | .I mode | |
928410b4 | 96 | string can also include the letter 'b' either as a last character or as |
fea681da | 97 | a character between the characters in any of the two-character strings |
c13182ef MK |
98 | described above. |
99 | This is strictly for compatibility with C89 | |
928410b4 | 100 | and has no effect; the 'b' is ignored on all POSIX |
fea681da MK |
101 | conforming systems, including Linux. |
102 | (Other systems may treat text files and binary files differently, | |
928410b4 | 103 | and adding the 'b' may be a good idea if you do I/O to a binary |
fea681da MK |
104 | file and expect that your program may be ported to non-Unix |
105 | environments.) | |
106 | .PP | |
107 | Any created files will have mode | |
108 | .BR S_IRUSR \&| S_IWUSR \&| S_IRGRP \&| S_IWGRP \&| S_IROTH \&| S_IWOTH | |
fc7ba057 | 109 | (0666), as modified by the process's umask value (see |
fea681da MK |
110 | .BR umask (2)). |
111 | .PP | |
112 | Reads and writes may be intermixed on read/write streams in any order. | |
113 | Note that ANSI C requires that a file positioning function intervene | |
114 | between output and input, unless an input operation encounters end-of-file. | |
115 | (If this condition is not met, then a read is allowed to return the | |
116 | result of writes other than the most recent.) | |
117 | Therefore it is good practice (and indeed sometimes necessary | |
118 | under Linux) to put an | |
fb186734 | 119 | .BR fseek (3) |
fea681da | 120 | or |
fb186734 | 121 | .BR fgetpos (3) |
c13182ef | 122 | operation between write and read operations on such a stream. |
2f0af33b MK |
123 | This operation may be an apparent no-op |
124 | (as in \fIfseek(..., 0L, SEEK_CUR)\fR | |
125 | called for its synchronizing side effect. | |
fea681da MK |
126 | .PP |
127 | Opening a file in append mode (\fBa\fR as the first character of | |
128 | .IR mode ) | |
129 | causes all subsequent write operations to this stream to occur | |
130 | at end-of-file, as if preceded by an | |
131 | .RS | |
132 | fseek(stream,0,SEEK_END); | |
133 | .RE | |
134 | call. | |
135 | .PP | |
136 | The | |
e511ffb6 | 137 | .BR fdopen () |
fea681da MK |
138 | function associates a stream with the existing file descriptor, |
139 | .IR fildes . | |
140 | The | |
141 | .I mode | |
142 | of the stream (one of the values "r", "r+", "w", "w+", "a", "a+") | |
143 | must be compatible with the mode of the file descriptor. | |
144 | The file position indicator of the new stream is set to that | |
145 | belonging to | |
146 | .IR fildes , | |
147 | and the error and end-of-file indicators are cleared. | |
148 | Modes "w" or "w+" do not cause truncation of the file. | |
149 | The file descriptor is not dup'ed, and will be closed when | |
150 | the stream created by | |
e511ffb6 | 151 | .BR fdopen () |
fea681da MK |
152 | is closed. |
153 | The result of applying | |
e511ffb6 | 154 | .BR fdopen () |
fea681da MK |
155 | to a shared memory object is undefined. |
156 | .PP | |
157 | The | |
e511ffb6 | 158 | .BR freopen () |
fea681da MK |
159 | function opens the file whose name is the string pointed to by |
160 | .I path | |
161 | and associates the stream pointed to by | |
162 | .I stream | |
c13182ef MK |
163 | with it. |
164 | The original stream (if it exists) is closed. | |
165 | The | |
fea681da MK |
166 | .I mode |
167 | argument is used just as in the | |
e511ffb6 | 168 | .BR fopen () |
c13182ef MK |
169 | function. |
170 | The primary use of the | |
e511ffb6 | 171 | .BR freopen () |
fea681da MK |
172 | function is to change the file associated with a standard text stream |
173 | .IR "" ( stderr ", " stdin ", or " stdout ). | |
174 | .SH "RETURN VALUE" | |
175 | Upon successful completion | |
e511ffb6 MK |
176 | .BR fopen (), |
177 | .BR fdopen () | |
fea681da | 178 | and |
e511ffb6 | 179 | .BR freopen () |
fea681da | 180 | return a |
836f07c1 | 181 | .I FILE |
c13182ef | 182 | pointer. |
8478ee02 | 183 | Otherwise, NULL is returned and the global variable |
fea681da MK |
184 | .I errno |
185 | is set to indicate the error. | |
186 | .SH ERRORS | |
187 | .TP | |
188 | .B EINVAL | |
189 | The | |
190 | .I mode | |
191 | provided to | |
e511ffb6 MK |
192 | .BR fopen (), |
193 | .BR fdopen (), | |
fea681da | 194 | or |
e511ffb6 | 195 | .BR freopen () |
fea681da MK |
196 | was invalid. |
197 | .PP | |
198 | The | |
e511ffb6 MK |
199 | .BR fopen (), |
200 | .BR fdopen () | |
fea681da | 201 | and |
e511ffb6 | 202 | .BR freopen () |
fea681da MK |
203 | functions may also fail and set |
204 | .I errno | |
205 | for any of the errors specified for the routine | |
206 | .BR malloc (3). | |
207 | .PP | |
208 | The | |
e511ffb6 | 209 | .BR fopen () |
fea681da MK |
210 | function may also fail and set |
211 | .I errno | |
212 | for any of the errors specified for the routine | |
213 | .BR open (2). | |
214 | .PP | |
215 | The | |
e511ffb6 | 216 | .BR fdopen () |
fea681da MK |
217 | function may also fail and set |
218 | .I errno | |
219 | for any of the errors specified for the routine | |
220 | .BR fcntl (2). | |
221 | .PP | |
222 | The | |
e511ffb6 | 223 | .BR freopen () |
fea681da MK |
224 | function may also fail and set |
225 | .I errno | |
226 | for any of the errors specified for the routines | |
227 | .BR open (2), | |
228 | .BR fclose (3) | |
229 | and | |
230 | .BR fflush (3). | |
231 | .SH "CONFORMING TO" | |
232 | The | |
e511ffb6 | 233 | .BR fopen () |
fea681da | 234 | and |
e511ffb6 | 235 | .BR freopen () |
c13182ef | 236 | functions conform to C89. |
1eb85d14 | 237 | The |
e511ffb6 | 238 | .BR fdopen () |
68e1685c | 239 | function conforms to POSIX.1-1990. |
d597239c MK |
240 | .SH NOTES |
241 | .SS Glibc Notes | |
3ed4fcbd | 242 | The GNU C library allows the following extensions for the string specified in |
a30e090e MK |
243 | .IR mode : |
244 | .TP | |
bcd70adc | 245 | .BR c " (since glibc 2.3.3)" |
c13182ef | 246 | Do not make the open operation, |
bcd70adc MK |
247 | or subsequent read and write operations, |
248 | thread cancellation points. | |
249 | .TP | |
250 | .BR m " (since glibc 2.3)" | |
c13182ef | 251 | Attempt to access the file using |
bcd70adc | 252 | .BR mmap (2), |
c13182ef | 253 | rather than I/O system calls |
bcd70adc MK |
254 | .RB ( read (2), |
255 | .BR write (2)). | |
c13182ef | 256 | Currently, |
bcd70adc | 257 | .\" As at glibc 2.4: |
2d2c82e1 | 258 | use of |
bcd70adc | 259 | .BR mmap (2) |
2d2c82e1 | 260 | is only attempted for a file opened for reading. |
bcd70adc | 261 | .TP |
a30e090e | 262 | .B x |
accdf645 | 263 | .\" Since glibc 2.0? |
a30e090e | 264 | Open the file exclusively |
c13182ef | 265 | (like the |
a30e090e MK |
266 | .B O_EXCL |
267 | flag of | |
268 | .BR open (2)). | |
269 | If the file already exists, | |
270 | .BR fopen () | |
271 | fails, and sets | |
272 | .I errno | |
273 | to | |
274 | .BR EEXIST . | |
bcd70adc MK |
275 | This flag is ignored for |
276 | .BR fdopen (). | |
218e46f8 | 277 | .\" FIXME document /,ccs= charset/ |
fea681da MK |
278 | .SH "SEE ALSO" |
279 | .BR open (2), | |
280 | .BR fclose (3), | |
281 | .BR fileno (3) |