Name
freopen
Synopsis
Changes the file associated with an existing file pointer
#include <stdio.h> FILE *freopen
( const char * restrictname
, const char * restrictmode
, FILE * restrictfp
);
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) );
}
Get C in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.