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

NAME

6       freopen - open a stream
7

SYNOPSIS

9       #include <stdio.h>
10
11       FILE *freopen(const char *restrict filename, const char *restrict mode,
12              FILE *restrict stream);
13
14

DESCRIPTION

16       The  freopen()  function  shall  first  attempt to flush the stream and
17       close any file descriptor associated with stream. Failure to  flush  or
18       close  the file descriptor successfully shall be ignored. The error and
19       end-of-file indicators for the stream shall be cleared.
20
21       The freopen() function shall open the file whose pathname is the string
22       pointed  to  by  filename and associate the stream pointed to by stream
23       with it. The mode argument shall be used just as in fopen().
24
25       The original stream shall be closed regardless of  whether  the  subse‐
26       quent open succeeds.
27
28       If  filename is a null pointer, the freopen() function shall attempt to
29       change the mode of the stream to that specified by mode, as if the name
30       of  the  file currently associated with the stream had been used. It is
31       implementation-defined which changes of mode are  permitted  (if  any),
32       and under what circumstances.
33
34       After  a  successful call to the freopen() function, the orientation of
35       the stream shall be cleared,    the encoding  rule  shall  be  cleared,
36       and the associated mbstate_t object shall be set to describe an initial
37       conversion state.
38
39       The largest value that can be represented correctly  in  an  object  of
40       type  off_t shall be established as the offset maximum in the open file
41       description.
42

RETURN VALUE

44       Upon successful completion, freopen() shall return the value of stream.
45       Otherwise,  a null pointer shall be returned,    and errno shall be set
46       to indicate the error.
47

ERRORS

49       The freopen() function shall fail if:
50
51       EACCES Search permission is denied on a component of the  path  prefix,
52              or  the  file  exists  and the permissions specified by mode are
53              denied, or the file does  not  exist  and  write  permission  is
54              denied for the parent directory of the file to be created.
55
56       EINTR  A signal was caught during freopen().
57
58       EISDIR The named file is a directory and mode requires write access.
59
60       ELOOP  A loop exists in symbolic links encountered during resolution of
61              the path argument.
62
63       EMFILE {OPEN_MAX} file descriptors are currently open  in  the  calling
64              process.
65
66       ENAMETOOLONG
67
68              The  length  of  the  filename  argument exceeds {PATH_MAX} or a
69              pathname component is longer than {NAME_MAX}.
70
71       ENFILE The maximum allowable number of files is currently open  in  the
72              system.
73
74       ENOENT A  component of filename does not name an existing file or file‐
75              name is an empty string.
76
77       ENOSPC The directory or file system that would  contain  the  new  file
78              cannot  be  expanded,  the file does not exist, and it was to be
79              created.
80
81       ENOTDIR
82              A component of the path prefix is not a directory.
83
84       ENXIO  The named file is a character special or block special file, and
85              the device associated with this special file does not exist.
86
87       EOVERFLOW
88              The named file is a regular file and the size of the file cannot
89              be represented correctly in an object of type off_t.
90
91       EROFS  The named file resides on  a  read-only  file  system  and  mode
92              requires write access.
93
94
95       The freopen() function may fail if:
96
97       EINVAL The value of the mode argument is not valid.
98
99       ELOOP  More  than  {SYMLOOP_MAX} symbolic links were encountered during
100              resolution of the path argument.
101
102       ENAMETOOLONG
103
104              Pathname resolution of a symbolic link produced an  intermediate
105              result whose length exceeds {PATH_MAX}.
106
107       ENOMEM Insufficient storage space is available.
108
109       ENXIO  A  request  was made of a nonexistent device, or the request was
110              outside the capabilities of the device.
111
112       ETXTBSY
113              The file is a pure procedure (shared text) file  that  is  being
114              executed and mode requires write access.
115
116
117       The following sections are informative.
118

EXAMPLES

120   Directing Standard Output to a File
121       The  following  example  logs  all  standard output to the /tmp/logfile
122       file.
123
124
125              #include <stdio.h>
126              ...
127              FILE *fp;
128              ...
129              fp = freopen ("/tmp/logfile", "a+", stdout);
130              ...
131

APPLICATION USAGE

133       The freopen() function  is  typically  used  to  attach  the  preopened
134       streams associated with stdin, stdout, and stderr to other files.
135

RATIONALE

137       None.
138

FUTURE DIRECTIONS

140       None.
141

SEE ALSO

143       fclose() , fopen() , fdopen() , mbsinit() , the Base Definitions volume
144       of IEEE Std 1003.1-2001, <stdio.h>
145
147       Portions of this text are reprinted and reproduced in  electronic  form
148       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
149       -- Portable Operating System Interface (POSIX),  The  Open  Group  Base
150       Specifications  Issue  6,  Copyright  (C) 2001-2003 by the Institute of
151       Electrical and Electronics Engineers, Inc and The Open  Group.  In  the
152       event of any discrepancy between this version and the original IEEE and
153       The Open Group Standard, the original IEEE and The Open Group  Standard
154       is  the  referee document. The original Standard can be obtained online
155       at http://www.opengroup.org/unix/online.html .
156
157
158
159IEEE/The Open Group                  2003                           FREOPEN(P)
Impressum