freopen() Function
The freopen()
function in C is used to reassign an existing stream to a different file or change its access mode. This is particularly useful for redirecting predefined streams, such as stdin
, stdout
, or stderr
, to specific files or altering their modes without creating a new stream.
Syntax of freopen()
FILE *freopen(const char *filename, const char *mode, FILE *stream);
Parameters
Parameter | Description |
---|---|
filename | A C string containing the name of the file to be opened. If filename is a null pointer, the mode of the stream is changed for the file currently associated with it. |
mode | A C string specifying the file access mode (e.g., "r" , "w" , "a" , "r+" , "w+" , or "a+" ). For binary files, include a "b" in the mode string. |
stream | A pointer to an existing FILE object that identifies the stream to be reopened. |
After closing any file currently associated with the stream, freopen()
opens the new file (if a filename is provided) and associates it with the stream. Additionally, the error and EOF indicators of the stream are cleared automatically.
Return Value
If the file is successfully reopened, the function returns the same pointer passed as the stream
parameter, allowing it to be used as before. Otherwise, it returns a null pointer, and the global variable errno
is set to indicate the error.
Examples for freopen()
Example 1: Redirecting stdout to a File
This example demonstrates how to redirect the standard output stream to a file, so that all output from printf()
is written to the file instead of the console.
Program
#include <stdio.h>
int main() {
// Redirect stdout to output.txt
if (freopen("output.txt", "w", stdout) == NULL) {
perror("freopen failed");
return 1;
}
printf("This output is redirected to output.txt\n");
return 0;
}
Explanation:
- The program attempts to redirect
stdout
to a file namedoutput.txt
usingfreopen()
. - If
freopen()
fails, an error message is printed usingperror()
and the program exits with an error code. - If successful, all subsequent output from
printf()
is written tooutput.txt
rather than the console.
Program Output:
This output is redirected to output.txt
Example 2: Redirecting stdin from a File
This example shows how to redirect the standard input stream from a file, allowing the program to read input from the file instead of the keyboard.
Program
#include <stdio.h>
int main() {
// Redirect stdin from input.txt
if (freopen("input.txt", "r", stdin) == NULL) {
perror("freopen failed");
return 1;
}
char buffer[100];
if (fgets(buffer, sizeof(buffer), stdin) != NULL) {
printf("Read from file: %s", buffer);
}
return 0;
}
Explanation:
- The program redirects
stdin
to read from a file namedinput.txt
usingfreopen()
. - If the redirection fails, an error message is printed and the program terminates.
- If successful, the program reads a line from
input.txt
into a buffer and then prints the read line to the standard output.
Program Output:
Read from file: (contents of input.txt)
Example 3: Changing Mode of an Open Stream
This example demonstrates how to change the access mode of an already opened stream by passing a null pointer as the filename. (Note that not all implementations support mode changes on an open stream.)
Program
#include <stdio.h>
int main() {
// Initially open a file in write mode
FILE *fp = fopen("example.txt", "w");
if (fp == NULL) {
perror("fopen failed");
return 1;
}
fprintf(fp, "Initial content.\n");
// Change the mode of the stream to read/update using freopen with a null filename
if (freopen(NULL, "r+", fp) == NULL) {
perror("freopen failed");
return 1;
}
// Reposition and read the content
char buffer[100];
rewind(fp);
if (fgets(buffer, sizeof(buffer), fp) != NULL) {
printf("File content after mode change: %s", buffer);
}
return 0;
}
Explanation:
- A file named
example.txt
is opened in write mode and initial content is written to it. - The
freopen()
function is then used with a null pointer as the filename to change the mode of the stream to read/update ("r+"
). - The file pointer is rewound to the beginning, and the content is read back and printed to verify the mode change.
Program Output:
File content after mode change: Initial content.