Compartilhar via


freopen, _wfreopen (Windows CE 5.0)

Send Feedback

Developing an Application > Microsoft C Run-time Library for Windows CE > Run-time Library Reference

Reassign a file pointer.

FILE *_wfreopen(   const wchar_t*path,constwchar_t*mode,FILE*stream);

Parameters

  • path
    Path of new file.
  • mode
    Type of access permitted.
  • stream
    Pointer to FILE structure.

Libraries

All versions of the C run-time libraries.

Return Value

This function 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.

Remarks

The _wfreopen function closes the file currently associated with stream and reassigns stream to the file specified by path. The path and mode arguments to _wfreopen are wide-character strings.

Generic-Text Routine Mappings

TCHAR.H Routine _UNICODE Defined
_tfreopen _wfreopen

For more information about TCHAR.H routines, see Generic Text Mappings.

_wfreopen 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:

  • "r"
    Opens for reading. If the file does not exist or cannot be found, the _wfreopen call fails.
  • "w"
    Opens an empty file for writing. If the given file exists, its contents are destroyed.
  • "a"
    Opens for writing at the end of the file (appending) without removing the EOF marker before writing new data to the file; creates the file first if it does not exist.
  • "r+"
    Opens for both reading and writing. (The file must exist.)
  • "w+"
    Opens an empty file for both reading and writing. If the given file exists, its contents are destroyed.
  • "a+"
    Opens for reading and appending; the appending operation includes the removal of the EOF marker before new data is written to the file and the EOF marker is restored after writing is complete; creates the file first if it does not exist.

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, 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, or fseek 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 can be included in the mode string to specify the translation mode for new lines.

  • t
    Open in text (translated) mode; carriage return–linefeed (CR-LF) combinations are translated into single linefeed (LF) characters on input; LF characters are translated to CR-LF combinations on output. Also, CTRL+Z is interpreted as an end-of-file character on input.

    In files opened for reading or for writing and reading with "a+", the run-time library checks for a CTRL+Z at the end of the file and removes it, if possible. This is done because using fseek and ftell to move within a file may cause fseek to behave improperly near the end of the file. The t option is a Microsoft extension that should not be used where ANSI portability is desired.

  • b
    Open in binary (untranslated) mode; the above translations are suppressed.

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.

Example

/* FREOPEN.C: 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;

void main( void )
{
   /* Reassign "stderr" to "freopen.out": */
   stream = freopen( "freopen.out", "w", stderr );

   if( stream == NULL )
      fprintf( stdout, "error on freopen\n" );
   else
   {
      fprintf( stream, "This will go to the file 'freopen.out'\n" );
      fprintf( stdout, "successfully reassigned\n" );
      fclose( stream );
   }
   system( "type freopen.out" );
}

Output

successfully reassigned
This will go to the file 'freopen.out'

See Also

fclose | _fileno | fopen | _setmode

Send Feedback on this topic to the authors

Feedback FAQs

© 2006 Microsoft Corporation. All rights reserved.