freopen, _wfreopen
Reassign a file pointer. More secure versions of these functions are available; see freopen_s, _wfreopen_s.
FILE *freopen( const char *path, const char *mode, FILE *stream ); FILE *_wfreopen( const wchar_t *path, const wchar_t *mode, FILE *stream );
Each of these functions returns a pointer to the newly opened file. If an error occurs, the original file is closed and the function returns a NULL pointer value. If path, mode, or stream is a null pointer, or if filename is an empty string, these functions invoke the invalid parameter handler, as described in Parameter Validation. If execution is allowed to continue, these functions set errno to EINVAL and return NULL.
See _doserrno, errno, _sys_errlist, and _sys_nerr for more information on these, and other, error codes.
More secure versions of these functions exist, see freopen_s, _wfreopen_s.
The freopen function closes the file currently associated with stream and reassigns stream to the file specified by path. _wfreopen is a wide-character version of _freopen; the path and mode arguments to _wfreopen are wide-character strings. _wfreopen and _freopen behave identically otherwise.
TCHAR.H routine | _UNICODE & _MBCS not defined | _MBCS defined | _UNICODE defined |
|---|---|---|---|
_tfreopen | freopen | freopen | _wfreopen |
freopen is typically used to redirect the pre-opened files stdin, stdout, and stderr to files specified by the user. The new file associated with stream is opened with mode, which is a character string specifying the type of access requested for the file, as follows:
Use the "w" and "w+" types with care, as they can destroy existing files.
When a file is opened with the "a" or "a+" access type, all write operations take place at the end of the file. Although the file pointer can be repositioned using fseek or rewind, the file pointer is always moved back to the end of the file before any write operation is carried out. Thus, existing data cannot be overwritten.
The "a" mode does not remove the EOF marker before appending to the file. After appending has occurred, the MS-DOS TYPE command only shows data up to the original EOF marker and not any data appended to the file. The "a+" mode does remove the EOF marker before appending to the file. After appending, the MS-DOS TYPE command shows all data in the file. The "a+" mode is required for appending to a stream file that is terminated with the CTRL+Z EOF marker.
When the "r+", "w+", or "a+" access type is specified, both reading and writing are allowed (the file is said to be open for "update"). However, when you switch between reading and writing, there must be an intervening fsetpos, fseek, or rewind operation. The current position can be specified for the fsetpos or fseek operation, if desired. In addition to the above values, one of the following characters may be included in the mode string to specify the translation mode for new lines.
If t or b is not given in mode, the default translation mode is defined by the global variable _fmode. If t or b is prefixed to the argument, the function fails and returns NULL.
For a discussion of text and binary modes, see Text and Binary Mode File I/O.
Function | Required header |
|---|---|
freopen | <stdio.h> |
_wfreopen | <stdio.h> or <wchar.h> |
For additional compatibility information, see Compatibility in the Introduction.
// crt_freopen.c
// compile with: /W3
// This program reassigns stderr to the file
// named FREOPEN.OUT and writes a line to that file.
#include <stdio.h>
#include <stdlib.h>
FILE *stream;
int main( void )
{
// Reassign "stderr" to "freopen.out":
stream = freopen( "freopen.out", "w", stderr ); // C4996
// Note: freopen is deprecated; consider using freopen_s instead
if( stream == NULL )
fprintf( stdout, "error on freopen\n" );
else
{
fprintf( stdout, "successfully reassigned\n" ); fflush( stdout );
fprintf( stream, "This will go to the file 'freopen.out'\n" );
fclose( stream );
}
system( "type freopen.out" );
}
successfully reassigned This will go to the file 'freopen.out'