Thursday, April 03, 2008

freopen()

FILE * freopen ( const char * filename, const char * mode, FILE * stream );

Reopen stream with different file or mode

freopen first tries to close any file already associated with the stream given as third parameter and disassociates it.
Then, whether that stream was successfuly closed or not, freopen opens the file whose name is passed in the first parameter, filename, and associates it with the specified stream just as fopen would do using the mode value specified as the second parameter.

Return Value

If the file was succesfully reopened, the function returns a pointer to an object identifying the stream. Otherwise, a null pointer is returned.

Example

/* freopen example: redirecting stdout */
#include

int main ()
{
freopen ("myfile.txt","w",stdout);
printf ("This sentence is redirected to a file.");
fclose (stdout);
return 0;
}

No comments: