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 *path, const char *mode);
12       FILE *fdopen(int fildes, const char *mode);
13       FILE *freopen(const char *path, const char *mode, FILE *stream);
14

DESCRIPTION

16       The fopen() function opens the file whose name is the string pointed to
17       by path and associates a stream with it.
18
19       The argument mode points to a string beginning with one of the  follow‐
20       ing sequences (Additional characters may follow these sequences.):
21
22       r      Open  text  file  for  reading.  The stream is positioned at the
23              beginning of the file.
24
25       r+     Open for reading and writing.  The stream is positioned  at  the
26              beginning of the file.
27
28       w      Truncate  file  to  zero length or create text file for writing.
29              The stream is positioned at the beginning of the file.
30
31       w+     Open for reading and writing.  The file is created  if  it  does
32              not  exist, otherwise it is truncated.  The stream is positioned
33              at the beginning of the file.
34
35       a      Open for appending (writing at end of file).  The file  is  cre‐
36              ated  if it does not exist.  The stream is positioned at the end
37              of the file.
38
39       a+     Open for reading and appending (writing at end  of  file).   The
40              file is created if it does not exist.  The initial file position
41              for reading is at the beginning  of  the  file,  but  output  is
42              always appended to the end of the file.
43
44       The  mode  string  can  also  include the letter ``b'' either as a last
45       character or as a character between the characters in any of  the  two-
46       character  strings described above.  This is strictly for compatibility
47       with C89 and has no effect; the ``b'' is ignored on all POSIX  conform‐
48       ing  systems, including Linux.  (Other systems may treat text files and
49       binary files differently, and adding the ``b'' may be a  good  idea  if
50       you  do I/O to a binary file and expect that your program may be ported
51       to non-Unix environments.)
52
53       Any        created         files         will         have         mode
54       S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH  (0666), as modified by
55       the process' umask value (see umask(2)).
56
57       Reads and writes may be intermixed on read/write streams in any  order.
58       Note  that  ANSI  C requires that a file positioning function intervene
59       between output and input, unless an input operation encounters  end-of-
60       file.   (If this condition is not met, then a read is allowed to return
61       the result of writes other than the most recent.)  Therefore it is good
62       practice (and indeed sometimes necessary under Linux) to put an fseek()
63       or fgetpos() operation between write and  read  operations  on  such  a
64       stream.   This operation may be an apparent no-op (as in fseek(..., 0L,
65       SEEK_CUR) called for its synchronizing side effect.
66
67       Opening a file in append mode (a as the first character of mode) causes
68       all subsequent write operations to this stream to occur at end-of-file,
69       as if preceded by an
70              fseek(stream,0,SEEK_END);
71       call.
72
73       The fdopen() function  associates  a  stream  with  the  existing  file
74       descriptor,  fildes.   The  mode  of the stream (one of the values "r",
75       "r+", "w", "w+", "a", "a+") must be compatible with  the  mode  of  the
76       file  descriptor.  The file position indicator of the new stream is set
77       to that belonging to fildes, and the error and  end-of-file  indicators
78       are  cleared.   Modes  "w" or "w+" do not cause truncation of the file.
79       The file descriptor is not dup'ed, and will be closed when  the  stream
80       created  by  fdopen()  is closed.  The result of applying fdopen() to a
81       shared memory object is undefined.
82
83       The freopen() function opens the file whose name is the string  pointed
84       to by path and associates the stream pointed to by stream with it.  The
85       original stream (if it exists) is closed.  The mode  argument  is  used
86       just  as  in  the  fopen()  function.  The primary use of the freopen()
87       function is to change the file associated with a standard  text  stream
88       (stderr, stdin, or stdout).
89

RETURN VALUE

91       Upon  successful  completion  fopen(),  fdopen() and freopen() return a
92       FILE pointer.  Otherwise, NULL is  returned  and  the  global  variable
93       errno is set to indicate the error.
94

ERRORS

96       EINVAL The  mode  provided  to  fopen(),  fdopen(),  or  freopen()  was
97              invalid.
98
99       The fopen(), fdopen() and freopen() functions may  also  fail  and  set
100       errno for any of the errors specified for the routine malloc(3).
101
102       The  fopen() function may also fail and set errno for any of the errors
103       specified for the routine open(2).
104
105       The fdopen() function may also fail and set errno for any of the errors
106       specified for the routine fcntl(2).
107
108       The  freopen()  function  may  also  fail  and set errno for any of the
109       errors specified for the routines open(2), fclose(3) and fflush(3).
110

CONFORMING TO

112       The fopen() and freopen() functions conform to C89.  The fdopen() func‐
113       tion conforms to POSIX.1-1990.
114

GLIBC EXTENSIONS

116       The GNU C library allows the following extensions for the string speci‐
117       fied in mode:
118
119       c (since glibc 2.3.3)
120              Do not make the open operation, or  subsequent  read  and  write
121              operations, thread cancellation points.
122
123       m (since glibc 2.3)
124              Attempt to access the file using mmap(2), rather than I/O system
125              calls (read(2), write(2)).  Currently, use of  mmap(2)  is  only
126              attempted for a file opened for reading.
127
128       x      Open the file exclusively (like the O_EXCL flag of open(2)).  If
129              the file already exists, fopen() fails, and sets errno  to  EEX‐
130              IST.  This flag is ignored for fdopen().
131

SEE ALSO

133       open(2), fclose(3), fileno(3)
134
135
136
137BSD MANPAGE                       2006-05-04                          FOPEN(3)
Impressum