Previous | Home | Next |
Description
The C library function, The freopen() function first closes the file that may currently be associated with stream. However, if the attempt to close the file fails, the freopen( ) function still continues to open the other file.
Declaration
Following is the declaration for freopen() function
FILE *freopen(const char *filename, const char *mode, FILE *stream)
Parameters
filename - filename is a char pointer referencing the name of a file that you want to associate with the standard stream represented by stream.
mode - mode is another char pointer pointing to a string that defines the way to open a file. The values that mode can have in freopen() are the same as the mode values in the fopen() function.
mode | description |
"r" | open an existing file for reading only |
"w" | open a new file for writing only. |
"a" | open an existing file for appending. |
"r+" | open an existing file for both reading and writing. |
"w+" | open a new file for both reading and writing. |
"a+" | open an existing file for both reading and appending. |
Stream - The stream to associate with filename.
Return Value
The freopen( ) function returns a pointer to stream on success and a null pointer otherwise.
Example
#include <stdio.h> int main () { freopen ("file.txt","w",stdout); printf ("This sentence is redirected."); fclose (stdout); return 0; }
Output
This sentence is redirected.
Previous | Home | Next |