1FREOPEN(3P) POSIX Programmer's Manual FREOPEN(3P)
2
3
4
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
12 freopen - open a stream
13
15 #include <stdio.h>
16
17 FILE *freopen(const char *restrict filename, const char *restrict mode,
18 FILE *restrict stream);
19
20
22 The freopen() function shall first attempt to flush the stream and
23 close any file descriptor associated with stream. Failure to flush or
24 close the file descriptor successfully shall be ignored. The error and
25 end-of-file indicators for the stream shall be cleared.
26
27 The freopen() function shall open the file whose pathname is the string
28 pointed to by filename and associate the stream pointed to by stream
29 with it. The mode argument shall be used just as in fopen().
30
31 The original stream shall be closed regardless of whether the subse‐
32 quent open succeeds.
33
34 If filename is a null pointer, the freopen() function shall attempt to
35 change the mode of the stream to that specified by mode, as if the name
36 of the file currently associated with the stream had been used. It is
37 implementation-defined which changes of mode are permitted (if any),
38 and under what circumstances.
39
40 After a successful call to the freopen() function, the orientation of
41 the stream shall be cleared, the encoding rule shall be cleared, and
42 the associated mbstate_t object shall be set to describe an initial
43 conversion state.
44
45 The largest value that can be represented correctly in an object of
46 type off_t shall be established as the offset maximum in the open file
47 description.
48
50 Upon successful completion, freopen() shall return the value of stream.
51 Otherwise, a null pointer shall be returned, and errno shall be set to
52 indicate the error.
53
55 The freopen() function shall fail if:
56
57 EACCES Search permission is denied on a component of the path prefix,
58 or the file exists and the permissions specified by mode are
59 denied, or the file does not exist and write permission is
60 denied for the parent directory of the file to be created.
61
62 EINTR A signal was caught during freopen().
63
64 EISDIR The named file is a directory and mode requires write access.
65
66 ELOOP A loop exists in symbolic links encountered during resolution of
67 the path argument.
68
69 EMFILE {OPEN_MAX} file descriptors are currently open in the calling
70 process.
71
72 ENAMETOOLONG
73
74 The length of the filename argument exceeds {PATH_MAX} or a
75 pathname component is longer than {NAME_MAX}.
76
77 ENFILE The maximum allowable number of files is currently open in the
78 system.
79
80 ENOENT A component of filename does not name an existing file or file‐
81 name is an empty string.
82
83 ENOSPC The directory or file system that would contain the new file
84 cannot be expanded, the file does not exist, and it was to be
85 created.
86
87 ENOTDIR
88 A component of the path prefix is not a directory.
89
90 ENXIO The named file is a character special or block special file, and
91 the device associated with this special file does not exist.
92
93 EOVERFLOW
94 The named file is a regular file and the size of the file cannot
95 be represented correctly in an object of type off_t.
96
97 EROFS The named file resides on a read-only file system and mode
98 requires write access.
99
100
101 The freopen() function may fail if:
102
103 EINVAL The value of the mode argument is not valid.
104
105 ELOOP More than {SYMLOOP_MAX} symbolic links were encountered during
106 resolution of the path argument.
107
108 ENAMETOOLONG
109
110 Pathname resolution of a symbolic link produced an intermediate
111 result whose length exceeds {PATH_MAX}.
112
113 ENOMEM Insufficient storage space is available.
114
115 ENXIO A request was made of a nonexistent device, or the request was
116 outside the capabilities of the device.
117
118 ETXTBSY
119 The file is a pure procedure (shared text) file that is being
120 executed and mode requires write access.
121
122
123 The following sections are informative.
124
126 Directing Standard Output to a File
127 The following example logs all standard output to the /tmp/logfile
128 file.
129
130
131 #include <stdio.h>
132 ...
133 FILE *fp;
134 ...
135 fp = freopen ("/tmp/logfile", "a+", stdout);
136 ...
137
139 The freopen() function is typically used to attach the preopened
140 streams associated with stdin, stdout, and stderr to other files.
141
143 None.
144
146 None.
147
149 fclose(), fopen(), fdopen(), mbsinit(), the Base Definitions volume of
150 IEEE Std 1003.1-2001, <stdio.h>
151
153 Portions of this text are reprinted and reproduced in electronic form
154 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
155 -- Portable Operating System Interface (POSIX), The Open Group Base
156 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
157 Electrical and Electronics Engineers, Inc and The Open Group. In the
158 event of any discrepancy between this version and the original IEEE and
159 The Open Group Standard, the original IEEE and The Open Group Standard
160 is the referee document. The original Standard can be obtained online
161 at http://www.opengroup.org/unix/online.html .
162
163
164
165IEEE/The Open Group 2003 FREOPEN(3P)