Re-directs a stream to another file.
#include <stdio.h> FILE *freopen(const char *filename, const char *mode, FILE *stream);
filename
A pointer to a character string containing the name of the file to open.
mode
A pointer to a character string specifying the operations be performed.
stream
A pointer to a file stream.
The freopen() function changes the file that stream is associated with to another file. The function first closes the file the stream is associated with, and opens the new file, filename, with the specified mode, using the same stream.
fopen() returns the value of stream, if it is successful. If fopen() fails it returns a null pointer ( NULL).
#include <stdio.h> #include <stdlib.h> int main(void) { FILE *f; // re-direct output from the console to a new file if (( f = freopen("newstdout", "w+", stdout)) == NULL) { printf("Can't create new stdout file.\n"); exit(1); } printf("If all goes well, this text should be in\n"); printf("a text file, not on the screen via stdout.\n"); fclose(f); return 0; }