freopen
Associates a new file with an open stream.
Defined in header <stdio.h>
Syntax
#include <stdio.h>
FILE *freopen(const char *filename, const char *mode, FILE *stream)',
Portability
| DOS | UNIX | Windows | ANSI C | C++ only |
|---|---|---|---|---|
| ■ | ■ | ■ | ■ |
Remarks
freopen substitutes the named file in place of the open stream. It closes stream, regardless of whether the open succeeds, freopen is useful for changing the file attached to stdin, stdout, or stderr.
The mode string used in calls to fopen is one of the following values:
r Open for reading only.
w Create for writing.
a Append; open for writing at end-of-file, or create for writing if the file does not exist.
r+ Open an existing file for update (reading and writing).
w+ Create a new file for update.
fl+ Open for append; open (or create if the file does not exist) for update at the end of the file.
To specify that a given file is being opened or created in text mode, append a f to the mode string (jt, zv+t, and so on); similarly, to specify binary mode, append a fc to the mode string {wb, a+b, and so on).
If a f or & is not given in the mode string, the mode is governed by the
| global variable _fmode. If _fmode is set to O_BINARY, | files are opened in |
binary mode. If _fmode is set to O_TEXT, they are opened in text mode. These O_... constants are defined in fcntl.h.
When a file is opened for update, both input and output can be done on the resulting stream. However, output cannot be directly followed by input without an intervening fseek or rewind, and input cannot be directly followed by output without an intervening fseek, rewind, or an input that encounters end-of-file.
Return value
On successful completion, freopen returns the argument stream. In the event of error, it returns null.
See also
Example
#include <stdio.h>
int main(void)
/* redirect standard output to a file */
if (freopen("OUTPUT.FIL", "w", stdout) == NULL)
fprintf(stderr, ■ "error redirecting stdout\n");
/* this output will go to a file */
printfC'This will go into a file.");
/* close the standard output stream */
fclose(stdout);
return 0;
Differences from modern implementations
Nothing recorded yet.
Modern references
These are search links, not yet checked by hand.
Stable link: /3.1/stdio.h/freopen/
· short form /3.1/freopen/