Previous Page
Next Page

freopen

Changes the file associated with an existing file pointer

#include <stdio.h>
FILE *freopen ( const char * restrict name , const char * restrict mode ,
               FILE * restrict fp  );

The freopen( ) function closes the file associated with the FILE pointer argument and opens the file with the specified name, associating it with the same FILE pointer as the file just closed. That FILE pointer is the function's return value. If an error occurs, freopen( ) returns a null pointer, and the FILE pointer passed to the function is closed.

The new access mode is specified by the second character string argument, in the same way described under fopen( ).

The most common use of freopen( ) is to redirect the standard I/O streams stdin, stdout, and stderr.

Example

time_t sec;
char fname[ ] = "test.dat";
if ( freopen( fname, "w", stdout ) == NULL )
  fprintf( stderr, "Unable to redirect stdout.\n" );
else
{
  time(&sec);
  printf( "%.24s: This file opened as stdout.\n", ctime(&sec) );
}

See Also

fopen( ), fclose( ), fflush( ), setbuf( )


Previous Page
Next Page