Penguin
Annotated edit history of fopen(3) version 3, including all changes. View license author blame.
Rev Author # Line
1 perry 1 FOPEN
2 !!!FOPEN
3 NAME
4 SYNOPSIS
5 DESCRIPTION
6 RETURN VALUE
7 ERRORS
8 CONFORMING TO
9 SEE ALSO
10 ----
11 !!NAME
12
13
14 fopen, fdopen, freopen - stream open functions
15 !!SYNOPSIS
16
17
18 __#include __
19
20
3 JimSmith 21 __FILE *fopen (const char *__''path''__, const char *__''mode''__);__%%%
22 __FILE *fdopen (int__ ''fildes''__, const char *__''mode''__);__%%%
2 JimSmith 23 __FILE *freopen (const char *__''path''__, const char *__''mode''__, FILE *__''stream''__);__
1 perry 24 !!DESCRIPTION
25
26
27 The __fopen__ function opens the file whose name is the
28 string pointed to by ''path'' and associates a stream
29 with it.
30
31
32 The argument ''mode'' points to a string beginning with
33 one of the following sequences (Additional characters may
34 follow these sequences.):
35
36
37 __r__
38
39
40 Open text file for reading. The stream is positioned at the
41 beginning of the file.
42
43
44 __r+__
45
46
47 Open for reading and writing. The stream is positioned at
48 the beginning of the file.
49
50
51 __w__
52
53
54 Truncate file to zero length or create text file for
55 writing. The stream is positioned at the beginning of the
56 file.
57
58
59 __w+__
60
61
62 Open for reading and writing. The file is created if it does
63 not exist, otherwise it is truncated. The stream is
64 positioned at the beginning of the file.
65
66
67 __a__
68
69
70 Open for writing. The file is created if it does not exist.
71 The stream is positioned at the end of the
72 file.
73
74
75 __a+__
76
77
78 Open for reading and writing. The file is created if it does
79 not exist. The stream is positioned at the end of the
80 file.
81
82
83 The ''mode'' string can also include the letter ``b''
84 either as a last character or as a character between the
85 characters in any of the two-character strings described
86 above. This is strictly for compatibility with ANSI
87 X3.159-1989 (``ANSI C'') and has no effect; the ``b'' is
88 ignored on all POSIX conforming systems, including Linux.
89 (Other systems may treat text files and binary files
90 differently, and adding the ``b'' may be a good idea if you
91 do I/O to a binary file and expect that your program may be
92 ported to non-Unix environments.)
93
94
95 Any created files will have mode
96 __S_IRUSR__|__S_IWUSR__|__S_IRGRP__|__S_IWGRP__|__S_IROTH__|__S_IWOTH__
97 (0666), as modified by the process' umask value (see
98 umask(2).
99
100
101 Reads and writes may be intermixed on read/write streams in
102 any order. Note that ANSI C requires that a file positioning
103 function intervene between output and input, unless an input
104 operation encounters end-of-file. (If this condition is not
105 met, then a read is allowed to return the result of writes
106 other than the most recent.) Therefore it is good practice
107 (and indeed sometimes necessary under Linux) to put an
108 __fseek__ or __fgetpos__ operation between write and
109 read operations on such a stream. This operation may be an
110 apparent no-op (as in ''fseek(..., 0L, SEEK_CUR)'' called
111 for its synchronizing side effect.
112
113
114 The __fdopen__ function associates a stream with the
115 existing file descriptor, ''fildes''. The ''mode'' of
116 the stream (one of the values
117 ''fildes'', and the error and
118 end-of-file indicators are cleared. Modes
119 ''fdopen__ is closed. The result of applying
120 __fdopen__ to a shared memory object is
121 undefined.
122
123
124 The __freopen__ function opens the file whose name is the
125 string pointed to by ''path'' and associates the stream
126 pointed to by ''stream'' with it. The original stream (if
127 it exists) is closed. The ''mode'' argument is used just
128 as in the __fopen__ function. The primary use of the
129 __freopen__ function is to change the file associated
130 with a standard text stream (''stderr'', ''stdin'', or
131 ''stdout'').
132 !!RETURN VALUE
133
134
135 Upon successful completion __fopen__, __fdopen__ and
136 __freopen__ return a __FILE__ pointer. Otherwise,
137 __NULL__ is returned and the global variable ''errno''
138 is set to indicate the error.
139 !!ERRORS
140
141
142 __EINVAL__
143
144
145 The ''mode'' provided to __fopen__, __fdopen__, or
146 __freopen__ was invalid.
147
148
149 The __fopen__, __fdopen__ and __freopen__ functions
150 may also fail and set ''errno'' for any of the errors
151 specified for the routine malloc(3).
152
153
154 The __fopen__ function may also fail and set ''errno''
155 for any of the errors specified for the routine
156 open(2).
157
158
159 The __fdopen__ function may also fail and set
160 ''errno'' for any of the errors specified for the routine
161 fcntl(2).
162
163
164 The __freopen__ function may also fail and set
165 ''errno'' for any of the errors specified for the
166 routines open(2), fclose(3) and
167 fflush(3).
168 !!CONFORMING TO
169
170
171 The __fopen__ and __freopen__ functions conform to
172 ANSI X3.159-1989 (``ANSI C''). The __fdopen__ function
173 conforms to IEEE Std1003.1-1988 (``POSIX.1'').
174 !!SEE ALSO
175
176
177 open(2), fclose(3),
178 fileno(3)
179 ----
This page is a man page (or other imported legacy content). We are unable to automatically determine the license status of this page.