1FOPEN(3)                   Linux Programmer's Manual                  FOPEN(3)
2
3
4

NAME

6       fopen, fdopen, freopen - stream open functions
7

SYNOPSIS

9       #include <stdio.h>
10
11       FILE *fopen(const char *pathname, const char *mode);
12
13       FILE *fdopen(int fd, const char *mode);
14
15       FILE *freopen(const char *pathname, const char *mode, FILE *stream);
16
17   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
18
19       fdopen(): _POSIX_C_SOURCE
20

DESCRIPTION

22       The fopen() function opens the file whose name is the string pointed to
23       by pathname and associates a stream with it.
24
25       The argument mode points to a string beginning with one of the  follow‐
26       ing sequences (possibly followed by additional characters, as described
27       below):
28
29       r      Open text file for reading.  The stream  is  positioned  at  the
30              beginning of the file.
31
32       r+     Open  for  reading and writing.  The stream is positioned at the
33              beginning of the file.
34
35       w      Truncate file to zero length or create text  file  for  writing.
36              The stream is positioned at the beginning of the file.
37
38       w+     Open  for  reading  and writing.  The file is created if it does
39              not exist, otherwise it is truncated.  The stream is  positioned
40              at the beginning of the file.
41
42       a      Open  for  appending (writing at end of file).  The file is cre‐
43              ated if it does not exist.  The stream is positioned at the  end
44              of the file.
45
46       a+     Open  for  reading  and appending (writing at end of file).  The
47              file is created if it does not exist.  The initial file position
48              for  reading  is  at  the  beginning  of the file, but output is
49              always appended to the end of the file.
50
51       The mode string can also include the letter 'b' either as a last  char‐
52       acter  or as a character between the characters in any of the two-char‐
53       acter strings described above.  This is strictly for compatibility with
54       C89  and has no effect; the 'b' is ignored on all POSIX conforming sys‐
55       tems, including Linux.  (Other systems may treat text files and  binary
56       files  differently, and adding the 'b' may be a good idea if you do I/O
57       to a binary file and expect that your program may be ported to non-UNIX
58       environments.)
59
60       See NOTES below for details of glibc extensions for mode.
61
62       Any  created  file  will  have  the  mode S_IRUSR | S_IWUSR | S_IRGRP |
63       S_IWGRP | S_IROTH | S_IWOTH (0666), as modified by the process's  umask
64       value (see umask(2)).
65
66       Reads  and writes may be intermixed on read/write streams in any order.
67       Note that ANSI C requires that a file  positioning  function  intervene
68       between  output and input, unless an input operation encounters end-of-
69       file.  (If this condition is not met, then a read is allowed to  return
70       the result of writes other than the most recent.)  Therefore it is good
71       practice (and  indeed  sometimes  necessary  under  Linux)  to  put  an
72       fseek(3)  or  fgetpos(3) operation between write and read operations on
73       such a stream.   This  operation  may  be  an  apparent  no-op  (as  in
74       fseek(..., 0L, SEEK_CUR) called for its synchronizing side effect).
75
76       Opening a file in append mode (a as the first character of mode) causes
77       all subsequent write operations to this stream to occur at end-of-file,
78       as if preceded the call:
79
80           fseek(stream, 0, SEEK_END);
81
82       The  file  descriptor  associated  with the stream is opened as if by a
83       call to open(2) with the following flags:
84
85              ┌─────────────┬───────────────────────────────┐
86fopen() mode open() flags                  
87              ├─────────────┼───────────────────────────────┤
88r       │ O_RDONLY                      │
89              ├─────────────┼───────────────────────────────┤
90w       │ O_WRONLY | O_CREAT | O_TRUNC  │
91              ├─────────────┼───────────────────────────────┤
92a       │ O_WRONLY | O_CREAT | O_APPEND │
93              ├─────────────┼───────────────────────────────┤
94r+      │ O_RDWR                        │
95              ├─────────────┼───────────────────────────────┤
96w+      │ O_RDWR | O_CREAT | O_TRUNC    │
97              ├─────────────┼───────────────────────────────┤
98a+      │ O_RDWR | O_CREAT | O_APPEND   │
99              └─────────────┴───────────────────────────────┘
100   fdopen()
101       The fdopen() function  associates  a  stream  with  the  existing  file
102       descriptor,  fd.   The mode of the stream (one of the values "r", "r+",
103       "w", "w+", "a", "a+") must be compatible with  the  mode  of  the  file
104       descriptor.   The  file  position indicator of the new stream is set to
105       that belonging to fd, and the  error  and  end-of-file  indicators  are
106       cleared.   Modes  "w" or "w+" do not cause truncation of the file.  The
107       file descriptor is not dup'ed, and will be closed when the stream  cre‐
108       ated  by  fdopen()  is  closed.   The  result of applying fdopen() to a
109       shared memory object is undefined.
110
111   freopen()
112       The freopen() function opens the file whose name is the string  pointed
113       to  by pathname and associates the stream pointed to by stream with it.
114       The original stream (if it exists) is closed.   The  mode  argument  is
115       used just as in the fopen() function.
116
117       If  the pathname argument is a null pointer, freopen() changes the mode
118       of the stream to that specified in mode; that is, freopen() reopens the
119       pathname  that  is  associated  with the stream.  The specification for
120       this behavior was added in the C99 standard, which says:
121
122              In this case, the file descriptor  associated  with  the  stream
123              need  not  be  closed  if the call to freopen() succeeds.  It is
124              implementation-defined which changes of mode are  permitted  (if
125              any), and under what circumstances.
126
127       The primary use of the freopen() function is to change the file associ‐
128       ated with a standard text stream (stderr, stdin, or stdout).
129

RETURN VALUE

131       Upon successful completion fopen(), fdopen()  and  freopen()  return  a
132       FILE pointer.  Otherwise, NULL is returned and errno is set to indicate
133       the error.
134

ERRORS

136       EINVAL The  mode  provided  to  fopen(),  fdopen(),  or  freopen()  was
137              invalid.
138
139       The  fopen(),  fdopen()  and  freopen() functions may also fail and set
140       errno for any of the errors specified for the routine malloc(3).
141
142       The fopen() function may also fail and set errno for any of the  errors
143       specified for the routine open(2).
144
145       The fdopen() function may also fail and set errno for any of the errors
146       specified for the routine fcntl(2).
147
148       The freopen() function may also fail and  set  errno  for  any  of  the
149       errors specified for the routines open(2), fclose(3), and fflush(3).
150

ATTRIBUTES

152       For   an   explanation   of   the  terms  used  in  this  section,  see
153       attributes(7).
154
155       ┌─────────────────────────────┬───────────────┬─────────┐
156Interface                    Attribute     Value   
157       ├─────────────────────────────┼───────────────┼─────────┤
158fopen(), fdopen(), freopen() │ Thread safety │ MT-Safe │
159       └─────────────────────────────┴───────────────┴─────────┘

CONFORMING TO

161       fopen(), freopen(): POSIX.1-2001, POSIX.1-2008, C89, C99.
162
163       fdopen(): POSIX.1-2001, POSIX.1-2008.
164

NOTES

166   Glibc notes
167       The GNU C library allows the following extensions for the string speci‐
168       fied in mode:
169
170       c (since glibc 2.3.3)
171              Do  not  make  the  open operation, or subsequent read and write
172              operations, thread cancellation points.  This  flag  is  ignored
173              for fdopen().
174
175       e (since glibc 2.7)
176              Open  the  file  with  the O_CLOEXEC flag.  See open(2) for more
177              information.  This flag is ignored for fdopen().
178
179       m (since glibc 2.3)
180              Attempt to access the file using mmap(2), rather than I/O system
181              calls   (read(2),  write(2)).   Currently,  use  of  mmap(2)  is
182              attempted only for a file opened for reading.
183
184       x      Open the file exclusively (like the O_EXCL flag of open(2)).  If
185              the  file  already exists, fopen() fails, and sets errno to EEX‐
186              IST.  This flag is ignored for fdopen().
187
188       In addition to the above characters, fopen() and freopen() support  the
189       following syntax in mode:
190
191           ,ccs=string
192
193       The  given string is taken as the name of a coded character set and the
194       stream is marked as  wide-oriented.   Thereafter,  internal  conversion
195       functions  convert  I/O  to  and from the character set string.  If the
196       ,ccs=string syntax is not specified, then the wide-orientation  of  the
197       stream is determined by the first file operation.  If that operation is
198       a wide-character operation, the stream  is  marked  wide-oriented,  and
199       functions to convert to the coded character set are loaded.
200

BUGS

202       When  parsing for individual flag characters in mode (i.e., the charac‐
203       ters preceding the "ccs" specification), the  glibc  implementation  of
204       fopen()  and freopen() limits the number of characters examined in mode
205       to 7 (or, in glibc versions before 2.14, to 6, which was not enough  to
206       include possible specifications such as "rb+cmxe").  The current imple‐
207       mentation of fdopen() parses at most 5 characters in mode.
208

SEE ALSO

210       open(2), fclose(3), fileno(3), fmemopen(3),  fopencookie(3),  open_mem‐
211       stream(3)
212

COLOPHON

214       This  page  is  part of release 4.16 of the Linux man-pages project.  A
215       description of the project, information about reporting bugs,  and  the
216       latest     version     of     this    page,    can    be    found    at
217       https://www.kernel.org/doc/man-pages/.
218
219
220
221GNU                               2017-09-15                          FOPEN(3)
Impressum