1FOPEN(P) POSIX Programmer's Manual FOPEN(P)
2
3
4
6 fopen - open a stream
7
9 #include <stdio.h>
10
11 FILE *fopen(const char *restrict filename, const char *restrict mode);
12
13
15 The fopen() function shall open the file whose pathname is the string
16 pointed to by filename, and associates a stream with it.
17
18 The mode argument points to a string. If the string is one of the fol‐
19 lowing, the file shall be opened in the indicated mode. Otherwise, the
20 behavior is undefined.
21
22 r or rb
23 Open file for reading.
24
25 w or wb
26 Truncate to zero length or create file for writing.
27
28 a or ab
29 Append; open or create file for writing at end-of-file.
30
31 r+ or rb+ or r+b
32 Open file for update (reading and writing).
33
34 w+ or wb+ or w+b
35 Truncate to zero length or create file for update.
36
37 a+ or ab+ or a+b
38 Append; open or create file for update, writing at end-of-file.
39
40
41 The character 'b' shall have no effect, but is allowed for ISO C stan‐
42 dard conformance. Opening a file with read mode (r as the first char‐
43 acter in the mode argument) shall fail if the file does not exist or
44 cannot be read.
45
46 Opening a file with append mode (a as the first character in the mode
47 argument) shall cause all subsequent writes to the file to be forced to
48 the then current end-of-file, regardless of intervening calls to
49 fseek().
50
51 When a file is opened with update mode ( '+' as the second or third
52 character in the mode argument), both input and output may be performed
53 on the associated stream. However, the application shall ensure that
54 output is not directly followed by input without an intervening call to
55 fflush() or to a file positioning function ( fseek(), fsetpos(), or
56 rewind()), and input is not directly followed by output without an
57 intervening call to a file positioning function, unless the input oper‐
58 ation encounters end-of-file.
59
60 When opened, a stream is fully buffered if and only if it can be deter‐
61 mined not to refer to an interactive device. The error and end-of-file
62 indicators for the stream shall be cleared.
63
64 If mode is w, wb, a, ab, w+, wb+, w+b, a+, ab+, or a+b, and the file
65 did not previously exist, upon successful completion, the fopen() func‐
66 tion shall mark for update the st_atime, st_ctime, and st_mtime fields
67 of the file and the st_ctime and st_mtime fields of the parent direc‐
68 tory.
69
70 If mode is w, wb, w+, wb+, or w+b, and the file did previously exist,
71 upon successful completion, fopen() shall mark for update the st_ctime
72 and st_mtime fields of the file. The fopen() function shall allocate a
73 file descriptor as open() does.
74
75 After a successful call to the fopen() function, the orientation of the
76 stream shall be cleared, the encoding rule shall be cleared, and
77 the associated mbstate_t object shall be set to describe an initial
78 conversion state.
79
80 The largest value that can be represented correctly in an object of
81 type off_t shall be established as the offset maximum in the open file
82 description.
83
85 Upon successful completion, fopen() shall return a pointer to the
86 object controlling the stream. Otherwise, a null pointer shall be
87 returned, and errno shall be set to indicate the error.
88
90 The fopen() function shall fail if:
91
92 EACCES Search permission is denied on a component of the path prefix,
93 or the file exists and the permissions specified by mode are
94 denied, or the file does not exist and write permission is
95 denied for the parent directory of the file to be created.
96
97 EINTR A signal was caught during fopen().
98
99 EISDIR The named file is a directory and mode requires write access.
100
101 ELOOP A loop exists in symbolic links encountered during resolution of
102 the path argument.
103
104 EMFILE {OPEN_MAX} file descriptors are currently open in the calling
105 process.
106
107 ENAMETOOLONG
108
109 The length of the filename argument exceeds {PATH_MAX} or a
110 pathname component is longer than {NAME_MAX}.
111
112 ENFILE The maximum allowable number of files is currently open in the
113 system.
114
115 ENOENT A component of filename does not name an existing file or file‐
116 name is an empty string.
117
118 ENOSPC The directory or file system that would contain the new file
119 cannot be expanded, the file does not exist, and the file was to
120 be created.
121
122 ENOTDIR
123 A component of the path prefix is not a directory.
124
125 ENXIO The named file is a character special or block special file, and
126 the device associated with this special file does not exist.
127
128 EOVERFLOW
129 The named file is a regular file and the size of the file cannot
130 be represented correctly in an object of type off_t.
131
132 EROFS The named file resides on a read-only file system and mode
133 requires write access.
134
135
136 The fopen() function may fail if:
137
138 EINVAL The value of the mode argument is not valid.
139
140 ELOOP More than {SYMLOOP_MAX} symbolic links were encountered during
141 resolution of the path argument.
142
143 EMFILE {FOPEN_MAX} streams are currently open in the calling process.
144
145 EMFILE {STREAM_MAX} streams are currently open in the calling process.
146
147 ENAMETOOLONG
148
149 Pathname resolution of a symbolic link produced an intermediate
150 result whose length exceeds {PATH_MAX}.
151
152 ENOMEM Insufficient storage space is available.
153
154 ETXTBSY
155 The file is a pure procedure (shared text) file that is being
156 executed and mode requires write access.
157
158
159 The following sections are informative.
160
162 Opening a File
163 The following example tries to open the file named file for reading.
164 The fopen() function returns a file pointer that is used in subsequent
165 fgets() and fclose() calls. If the program cannot open the file, it
166 just ignores it.
167
168
169 #include <stdio.h>
170 ...
171 FILE *fp;
172 ...
173 void rgrep(const char *file)
174 {
175 ...
176 if ((fp = fopen(file, "r")) == NULL)
177 return;
178 ...
179 }
180
182 None.
183
185 None.
186
188 None.
189
191 fclose() , fdopen() , freopen() , the Base Definitions volume of
192 IEEE Std 1003.1-2001, <stdio.h>
193
195 Portions of this text are reprinted and reproduced in electronic form
196 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
197 -- Portable Operating System Interface (POSIX), The Open Group Base
198 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
199 Electrical and Electronics Engineers, Inc and The Open Group. In the
200 event of any discrepancy between this version and the original IEEE and
201 The Open Group Standard, the original IEEE and The Open Group Standard
202 is the referee document. The original Standard can be obtained online
203 at http://www.opengroup.org/unix/online.html .
204
205
206
207IEEE/The Open Group 2003 FOPEN(P)