1fopen(3C)                Standard C Library Functions                fopen(3C)
2
3
4

NAME

6       fopen - open a stream
7

SYNOPSIS

9       #include <stdio.h>
10
11       FILE *fopen(const char *filename, const char *mode);
12
13

DESCRIPTION

15       The  fopen()  function  opens  the  file  whose  pathname is the string
16       pointed to by filename, and associates a stream with it.
17
18
19       The argument mode points to a string beginning with one of the  follow‐
20       ing sequences:
21
22       r or rb             Open file for reading.
23
24
25       w or wb             Truncate  to  zero  length or create file for writ‐
26                           ing.
27
28
29       a or ab             Append; open or create file for writing at  end-of-
30                           file.
31
32
33       r+ or rb+ or r+b    Open file for update (reading and writing).
34
35
36       w+ or wb+ or w+b    Truncate to zero length or create file for update.
37
38
39       a+ or ab+ or a+b    Append;  open or create file for update, writing at
40                           end-of-file.
41
42
43
44       The character b has no effect, but is allowed for ISO C  standard  con‐
45       formance  (see  standards(5)).  Opening a file with read mode (r as the
46       first character in the mode argument) fails if the file does not  exist
47       or cannot be read.
48
49
50       Opening  a  file with append mode (a as the first character in the mode
51       argument) causes all subsequent writes to the file to be forced to  the
52       then current end-of-file, regardless of intervening calls to fseek(3C).
53       If two separate processes open the same file for append,  each  process
54       may  write  freely  to the file without fear of destroying output being
55       written by the other.  The output from the two processes will be inter‐
56       mixed in the file in the order in which it is written.
57
58
59       When  a file is opened with update mode (+ as the second or third char‐
60       acter in the mode argument), both input and output may be performed  on
61       the associated stream. However, output must not be directly followed by
62       input without an intervening call to fflush(3C) or to a file  position‐
63       ing  function  (  fseek(3C), fsetpos(3C) or rewind(3C)), and input must
64       not be directly followed by output without an  intervening  call  to  a
65       file  positioning  function, unless the input operation encounters end-
66       of-file.
67
68
69       When opened, a stream is fully buffered if and only if it can be deter‐
70       mined  not to refer to an interactive device. The error and end-of-file
71       indicators for the stream are cleared.
72
73
74       If mode begins with w or a and the file did not previously exist,  upon
75       successful  completion,  fopen()  function  will  mark  for  update the
76       st_atime, st_ctime and st_mtime fields of the file and the st_ctime and
77       st_mtime fields of the parent directory.
78
79
80       If  mode begins with w and the file did previously exist, upon success‐
81       ful completion, fopen() will mark for update the st_ctime and  st_mtime
82       fields of the file.  The fopen() function will allocate a file descrip‐
83       tor as open(2) does.
84
85
86       Normally, 32-bit applications return an EMFILE error when attempting to
87       associate  a  stream  with  a file accessed by a file descriptor with a
88       value greater than 255. If the last character  of  mode  is  F,  32-bit
89       applications will be allowed to associate a stream with a file accessed
90       by a file descriptor with a value greater  than  255.  A  FILE  pointer
91       obtained in this way must never be used by any code that might directly
92       access fields in the FILE structure. If the fields in the  FILE  struc‐
93       ture  are  used directly by 32-bit applications when the last character
94       of mode is F, data corruption could occur. See  the  USAGE  section  of
95       this manual page and the enable_extended_FILE_stdio(3C) manual page for
96       other options for enabling the extended FILE facility.
97
98
99       In 64-bit applications, the last character of mode is silently  ignored
100       if  it  is  F.   64-bit  applications are always allowed to associate a
101       stream with a file accessed by a file descriptor with any value.
102
103
104       The largest value that can be represented correctly  in  an  object  of
105       type  off_t  will be established as the offset maximum in the open file
106       description.
107

RETURN VALUES

109       Upon successful completion, fopen() returns a  pointer  to  the  object
110       controlling  the  stream.   Otherwise,  a  null pointer is returned and
111       errno is set to indicate the error.
112
113
114       The fopen() function may fail and not set errno if there  are  no  free
115       stdio streams.
116

ERRORS

118       The fopen() function will fail if:
119
120       EACCES          Search  permission is denied on a component of the path
121                       prefix, or the file exists and the  permissions  speci‐
122                       fied by mode are denied, or the file does not exist and
123                       write permission is denied for the parent directory  of
124                       the file to be created.
125
126
127       EINTR           A signal was caught during the execution of fopen().
128
129
130       EISDIR          The  named  file is a directory and mode requires write
131                       access.
132
133
134       ELOOP           Too many symbolic links were encountered  in  resolving
135                       path.
136
137
138       EMFILE          There are {OPEN_MAX} file descriptors currently open in
139                       the calling process.
140
141
142       ENAMETOOLONG    The length of the filename exceeds PATH_MAX or a  path‐
143                       name component is longer than NAME_MAX.
144
145
146       ENFILE          The maximum allowable number of files is currently open
147                       in the system.
148
149
150       ENOENT          A component of filename does not name an existing  file
151                       or filename is an empty string.
152
153
154       ENOSPC          The directory or file system that would contain the new
155                       file cannot be expanded, the file does not  exist,  and
156                       it was to be created.
157
158
159       ENOTDIR         A component of the path prefix is not a directory.
160
161
162       ENXIO           The  named file is a character special or block special
163                       file, and the device associated with this special  file
164                       does not exist.
165
166
167       EOVERFLOW       The current value of the file position cannot be repre‐
168                       sented correctly in an object of type fpos_t.
169
170
171       EROFS           The named file resides on a read-only file  system  and
172                       mode requires write access.
173
174
175
176       The fopen() function may fail if:
177
178       EINVAL          The value of the mode argument is not valid.
179
180
181       EMFILE          {FOPEN_MAX}  streams  are currently open in the calling
182                       process.
183
184                       {STREAM_MAX} streams are currently open in the  calling
185                       process.
186
187
188       ENAMETOOLONG    Pathname  resolution  of  a  symbolic  link produced an
189                       intermediate result whose length exceeds {PATH_MAX}.
190
191
192       ENOMEM          Insufficient storage space is available.
193
194
195       ETXTBSY         The file is a pure procedure (shared text) file that is
196                       being executed and mode requires write access.
197
198

USAGE

200       A process is allowed to have at least {FOPEN_MAX} stdio streams open at
201       a time. For 32-bit applications, however, the underlying ABIs  formerly
202       required  that  no file descriptor used to access the file underlying a
203       stdio stream have a value greater than 255. To maintain binary compati‐
204       bility  with  earlier  Solaris  releases,  this  limit still constrains
205       32-bit applications. However, when a 32-bit application is  aware  that
206       no  code  that  has access to the FILE pointer returned by fopen() will
207       use the FILE pointer to directly access any fields in the  FILE  struc‐
208       ture,  the  F  character  can be used as the last character in the mode
209       argument to circumvent this limit. Because it could lead to  data  cor‐
210       ruption,  the  F  character  in  mode  must never be used when the FILE
211       pointer might later be used by binary code unknown to the user.  The  F
212       character in mode is intended to be used by library functions that need
213       a FILE pointer to access data to process a user  request,  but  do  not
214       need  to  pass  the  FILE pointer back to the user. 32-bit applications
215       that have been inspected can use the extended FILE facility to  circum‐
216       vent  this  limit if the inspection shows that no FILE pointers will be
217       used to directly access FILE structure contents.
218
219
220       The fopen() function has a transitional interface for 64-bit file  off‐
221       sets.  See lf64(5).
222

ATTRIBUTES

224       See attributes(5) for descriptions of the following attributes:
225
226
227
228
229       ┌─────────────────────────────┬─────────────────────────────┐
230       │      ATTRIBUTE TYPE         │      ATTRIBUTE VALUE        │
231       ├─────────────────────────────┼─────────────────────────────┤
232       │Interface Stability          │See below.                   │
233       ├─────────────────────────────┼─────────────────────────────┤
234       │MT-Level                     │MT-Safe                      │
235       └─────────────────────────────┴─────────────────────────────┘
236
237
238       The F character in the mode argument is Evolving. In all other respects
239       this function is Standard.
240

SEE ALSO

242       enable_extended_FILE_stdio(3C),  fclose(3C),  fdopen(3C),   fflush(3C),
243       freopen(3C),  fsetpos(3C),  rewind(3C),  attributes(5),  lf64(5), stan‐
244       dards(5)
245
246
247
248SunOS 5.11                        18 Apr 2006                        fopen(3C)
Impressum