C Reference ยท stdio.h
C freopen() function
Reference for C freopen(): close a stream and reattach the same FILE pointer to another file, redirect stdout or stdin, and handle failure safely.
freopen() closes whatever a FILE * currently refers to and reopens that same FILE * on a different file and mode. Its most common job is redirecting a standard stream: after freopen("log.txt", "w", stdout), every printf() and puts() in the program writes into log.txt instead of the screen, without changing any of that code.
It can also change the mode of an already open stream when you pass NULL as the file name (support for that is implementation defined).
#include <stdio.h> FILE *freopen(const char *restrict path, const char *restrict mode, FILE *restrict stream);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
FILE *freopen(const char *restrict path, const char *restrict mode, FILE *restrict stream);
| Name | Description |
|---|---|
path |
The file to open. May be NULL to only change the mode of the existing stream (implementation defined). |
mode |
Same strings as fopen(): "r", "w", "a", "r+", with an optional b. |
stream |
The existing stream to reuse, often stdin, stdout or stderr. |
Returns: stream itself on success, or NULL on failure (and errno is set). On failure the original stream is already closed.
Errors / undefined behavior: the stream is closed first and errors from closing are ignored. If the open fails, stream is no longer usable: do not write to stdout afterwards (you cannot even print the error to it). Use stderr or perror(). Passing a stream that was not open is undefined behavior.
Usage notes
- Restoring the original
stdoutis not portable withfreopen(). On POSIX systems you can keep a copy of the descriptor withdup()and restore it withdup2(), or reopen"/dev/tty". If you need both destinations, give functions aFILE *outparameter instead. - Failure leaves the stream dead. Test the result; and never write
stdout = freopen(...)becausestdoutmay not be assignable. - Flush first if you are switching an output stream and want earlier output to reach the old destination (the call does close and flush it, but be explicit for clarity).
- The
FILE *stays the same. Pointers copied elsewhere remain valid. - Typical uses: logging all output of a test run to a file, feeding a program canned input from a file instead of the keyboard, and capturing
stderr. - Temporary files in lessons: the examples create files in a temporary directory and remove them; real programs should check
remove()'s result. - Alternatives: for in-memory capture see
fmemopenandopen_memstream; for shell-style redirection run the program asprog < in.txt > out.txt.
Related
fopen,fclose,stdin,stdoutandstderr,FILE,perror- Lessons: File handling basics, Text files