1FOPEN(3P)                  POSIX Programmer's Manual                 FOPEN(3P)
2
3
4

PROLOG

6       This  manual  page is part of the POSIX Programmer's Manual.  The Linux
7       implementation of this interface may differ (consult the  corresponding
8       Linux  manual page for details of Linux behavior), or the interface may
9       not be implemented on Linux.
10

NAME

12       fopen - open a stream
13

SYNOPSIS

15       #include <stdio.h>
16
17       FILE *fopen(const char *restrict filename, const char *restrict mode);
18
19

DESCRIPTION

21       The fopen() function shall open the file whose pathname is  the  string
22       pointed to by filename, and associates a stream with it.
23
24       The  mode argument points to a string. If the string is one of the fol‐
25       lowing, the file shall be opened in the indicated mode. Otherwise,  the
26       behavior is undefined.
27
28       r or rb
29              Open file for reading.
30
31       w or wb
32              Truncate to zero length or create file for writing.
33
34       a or ab
35              Append; open or create file for writing at end-of-file.
36
37       r+ or rb+ or r+b
38              Open file for update (reading and writing).
39
40       w+ or wb+ or w+b
41              Truncate to zero length or create file for update.
42
43       a+ or ab+ or a+b
44              Append; open or create file for update, writing at end-of-file.
45
46
47       The  character 'b' shall have no effect, but is allowed for ISO C stan‐
48       dard conformance.  Opening a file with read mode (r as the first  char‐
49       acter  in  the  mode argument) shall fail if the file does not exist or
50       cannot be read.
51
52       Opening a file with append mode (a as the first character in  the  mode
53       argument) shall cause all subsequent writes to the file to be forced to
54       the then  current  end-of-file,  regardless  of  intervening  calls  to
55       fseek().
56
57       When  a  file  is  opened with update mode ( '+' as the second or third
58       character in the mode argument), both input and output may be performed
59       on  the  associated  stream. However, the application shall ensure that
60       output is not directly followed by input without an intervening call to
61       fflush()  or  to  a  file positioning function ( fseek(), fsetpos(), or
62       rewind()), and input is not directly  followed  by  output  without  an
63       intervening call to a file positioning function, unless the input oper‐
64       ation encounters end-of-file.
65
66       When opened, a stream is fully buffered if and only if it can be deter‐
67       mined  not to refer to an interactive device. The error and end-of-file
68       indicators for the stream shall be cleared.
69
70       If mode is w, wb, a, ab, w+, wb+, w+b, a+, ab+, or a+b,  and  the  file
71       did not previously exist, upon successful completion, the fopen() func‐
72       tion shall mark for update the st_atime, st_ctime, and st_mtime  fields
73       of  the  file and the st_ctime and st_mtime fields of the parent direc‐
74       tory.
75
76       If mode is w, wb, w+, wb+, or w+b, and the file did  previously  exist,
77       upon  successful completion, fopen() shall mark for update the st_ctime
78       and st_mtime fields of the file. The fopen() function shall allocate  a
79       file descriptor as open() does.
80
81       After a successful call to the fopen() function, the orientation of the
82       stream shall be cleared,  the encoding rule shall be cleared,  and  the
83       associated mbstate_t object shall be set to describe an initial conver‐
84       sion state.
85
86       The largest value that can be represented correctly  in  an  object  of
87       type  off_t shall be established as the offset maximum in the open file
88       description.
89

RETURN VALUE

91       Upon successful completion, fopen()  shall  return  a  pointer  to  the
92       object  controlling  the  stream.  Otherwise,  a  null pointer shall be
93       returned,  and errno shall be set to indicate the error.
94

ERRORS

96       The fopen() function shall fail if:
97
98       EACCES Search permission is denied on a component of the  path  prefix,
99              or  the  file  exists  and the permissions specified by mode are
100              denied, or the file does  not  exist  and  write  permission  is
101              denied for the parent directory of the file to be created.
102
103       EINTR  A signal was caught during fopen().
104
105       EISDIR The named file is a directory and mode requires write access.
106
107       ELOOP  A loop exists in symbolic links encountered during resolution of
108              the path argument.
109
110       EMFILE {OPEN_MAX} file descriptors are currently open  in  the  calling
111              process.
112
113       ENAMETOOLONG
114
115              The  length  of  the  filename  argument exceeds {PATH_MAX} or a
116              pathname component is longer than {NAME_MAX}.
117
118       ENFILE The maximum allowable number of files is currently open  in  the
119              system.
120
121       ENOENT A  component of filename does not name an existing file or file‐
122              name is an empty string.
123
124       ENOSPC The directory or file system that would  contain  the  new  file
125              cannot be expanded, the file does not exist, and the file was to
126              be created.
127
128       ENOTDIR
129              A component of the path prefix is not a directory.
130
131       ENXIO  The named file is a character special or block special file, and
132              the device associated with this special file does not exist.
133
134       EOVERFLOW
135              The named file is a regular file and the size of the file cannot
136              be represented correctly in an object of type off_t.
137
138       EROFS  The named file resides on  a  read-only  file  system  and  mode
139              requires write access.
140
141
142       The fopen() function may fail if:
143
144       EINVAL The value of the mode argument is not valid.
145
146       ELOOP  More  than  {SYMLOOP_MAX} symbolic links were encountered during
147              resolution of the path argument.
148
149       EMFILE {FOPEN_MAX} streams are currently open in the calling process.
150
151       EMFILE {STREAM_MAX} streams are currently open in the calling process.
152
153       ENAMETOOLONG
154
155              Pathname resolution of a symbolic link produced an  intermediate
156              result whose length exceeds {PATH_MAX}.
157
158       ENOMEM Insufficient storage space is available.
159
160       ETXTBSY
161              The  file  is  a pure procedure (shared text) file that is being
162              executed and mode requires write access.
163
164
165       The following sections are informative.
166

EXAMPLES

168   Opening a File
169       The following example tries to open the file named  file  for  reading.
170       The  fopen() function returns a file pointer that is used in subsequent
171       fgets() and fclose() calls.  If the program cannot open  the  file,  it
172       just ignores it.
173
174
175              #include <stdio.h>
176              ...
177              FILE *fp;
178              ...
179              void rgrep(const char *file)
180              {
181              ...
182                  if ((fp = fopen(file, "r")) == NULL)
183                      return;
184              ...
185              }
186

APPLICATION USAGE

188       None.
189

RATIONALE

191       None.
192

FUTURE DIRECTIONS

194       None.
195

SEE ALSO

197       fclose(),   fdopen(),   freopen(),   the  Base  Definitions  volume  of
198       IEEE Std 1003.1-2001, <stdio.h>
199
201       Portions of this text are reprinted and reproduced in  electronic  form
202       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
203       -- Portable Operating System Interface (POSIX),  The  Open  Group  Base
204       Specifications  Issue  6,  Copyright  (C) 2001-2003 by the Institute of
205       Electrical and Electronics Engineers, Inc and The Open  Group.  In  the
206       event of any discrepancy between this version and the original IEEE and
207       The Open Group Standard, the original IEEE and The Open Group  Standard
208       is  the  referee document. The original Standard can be obtained online
209       at http://www.opengroup.org/unix/online.html .
210
211
212
213IEEE/The Open Group                  2003                            FOPEN(3P)
Impressum