freopen、_wfreopenfreopen, _wfreopen

重新分配文件指针。Reassigns a file pointer. 提供这些函数的更安全版本;请参阅 freopen_s、_wfreopen_sMore 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   


新文件的路径。Path of new file.

允许的访问类型。Type of access permitted.

指向 FILE 结构的指针。Pointer to FILE structure.

返回值Return Value

这些函数均返回指向新打开的文件的指针。Each of these functions returns a pointer to the newly opened file. 如果发生错误,则关闭原始文件,且该函数返回 NULL 指针值。If an error occurs, the original file is closed and the function returns a NULL pointer value. 如果 pathmodestream 是 null 指针,或 filename 是空字符串,这些函数将调用无效参数处理程序,如参数验证中所述。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. 如果允许执行继续,则这些功能将 errno 设置为 EINVAL 并返回 NULLIf execution is allowed to continue, these functions set errno to EINVAL and return NULL.

有关这些代码以及其他错误代码的详细信息,请参阅 _doserrno、errno、_sys_errlist 和 _sys_nerrSee _doserrno, errno, _sys_errlist, and _sys_nerr for more information on these, and other, error codes.


提供这些函数的更安全版本;请参阅 freopen_s、_wfreopen_sMore secure versions of these functions exist, see freopen_s, _wfreopen_s.

freopen函数关闭当前与关联的文件stream,并重新指定stream到指定的文件pathThe freopen function closes the file currently associated with stream and reassigns stream to the file specified by path. _wfreopen_freopen 的宽字符版本;_wfreopenpathmode 参数是宽字符串。_wfreopen is a wide-character version of _freopen; the path and mode arguments to _wfreopen are wide-character strings. 除此以外,_wfreopen_freopen 的行为完全相同。_wfreopen and _freopen behave identically otherwise.

一般文本例程映射Generic-Text Routine Mappings

TCHAR.H 例程TCHAR.H routine 未定义 _UNICODE 和 _MBCS_UNICODE & _MBCS not defined 已定义 _MBCS_MBCS defined 已定义 _UNICODE_UNICODE defined
_tfreopen freopen freopen _wfreopen

freopen 通常用于将预先打开的文件 stdinstdoutstderr 重定向到用户指定的文件。freopen is typically used to redirect the pre-opened files stdin, stdout, and stderr to files specified by the user. 与关联的新文件stream使用打开mode,后者是一个字符串,指定,如下所示为该文件,请求的访问类型: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:

打开以便读取。Opens for reading. 如果文件不存在或找不到,freopen 调用将失败。If the file does not exist or cannot be found, the freopen call fails.

打开用于写入的空文件。Opens an empty file for writing. 如果给定文件存在,则其内容会被销毁。If the given file exists, its contents are destroyed.

打开以在文件末尾写入(追加),从而无需在将新数据写入文件之前移除 EOF 标记;如果文件不存在,则先创建文件。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.

打开以便读取和写入。Opens for both reading and writing. (该文件必须存在。)(The file must exist.)

打开用于读取和写入的空文件。Opens an empty file for both reading and writing. 如果给定文件存在,则其内容会被销毁。If the given file exists, its contents are destroyed.

打开以进行读取和追加;追加操作包括在新数据写入到文件之前移除 EOF 标记并在写入完成后还原 EOF 标记;如果文件不存在,则先创建文件。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.

使用 "w""w+" 类型时要小心,因为它们可能会破坏现有文件。Use the "w" and "w+" types with care, as they can destroy existing files.

使用 "a""a+" 访问类型打开文件时,所有写入操作均将在文件末尾进行。When a file is opened with the "a" or "a+" access type, all write operations take place at the end of the file. 虽然使用 fseekrewind 可重新定位文件指针,但在执行任何写入操作前,文件指针将始终被移回文件末尾。因此,无法覆盖现有数据。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.

在 EOF 标记追加到文件之前,"a" 模式不会将其删除。The "a" mode does not remove the EOF marker before appending to the file. 在追加后,MS-DOS TYPE 命令只显示原始 EOF 标记之前的数据,不显示追加到文件的任何数据。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. 在 EOF 标记追加到文件之前,"a+" 模式会将其删除。The "a+" mode does remove the EOF marker before appending to the file. 在追加后,MS-DOS TYPE 命令显示文件中的所有数据。After appending, the MS-DOS TYPE command shows all data in the file. 需使用 "a+" 模式才能附加到通过 CTRL+Z EOF 标记终止的流文件。The "a+" mode is required for appending to a stream file that is terminated with the CTRL+Z EOF marker.

指定 "r+""w+""a+" 访问类型时,允许读取和写入(文件将处于打开状态以进行“更新”)。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"). 但是,在读取与写入之间切换时,必须有干预性的 fsetposfseekrewind 操作。However, when you switch between reading and writing, there must be an intervening fsetpos, fseek, or rewind operation. 如果需要的话,可以为 fsetposfseek 操作指定当前位置。The current position can be specified for the fsetpos or fseek operation, if desired. 除上述值之外,还可以在 mode 字符串中包含以下字符之一以指定新行的转换模式。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.

在文本 (转换) 模式下打开;回车换行符 (CR-LF) 组合将转换为单一的换行 (LF) 字符输入;LF 字符将转换为 CR-LF 组合输出。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. CTRL+Z 也将在输入时解释为文件尾字符。Also, CTRL+Z is interpreted as an end-of-file character on input. 在打开以便利用 "a+" 进行读取或读写的文件中,运行时库将检查文件末尾的 CTRL+Z,并在可能的情况下将其删除。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. 这是因为使用 fseekftell 在文件中移动时,可能导致 fseek 在文件末尾附近错误运行。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. t 选项是一个 Microsoft 扩展,不应在需要 ANSI 可移植性的地方使用。The t option is a Microsoft extension that should not be used where ANSI portability is desired.

在二进制(未转换)模式下打开;禁止上述转换。Open in binary (untranslated) mode; the above translations are suppressed.

如果 tbmode中未给出,则默认转换模式由全局变量 _fmode定义。If t or b is not given in mode, the default translation mode is defined by the global variable _fmode. 如果 tb 是该参数的前缀,则函数将失败并返回 NULLIf t or b is prefixed to the argument, the function fails and returns NULL.

有关文本模式和二进制模式的讨论,请参阅文本和二进制模式文件 I/OFor a discussion of text and binary modes, see Text and Binary Mode File I/O.


函数Function 必需的标头Required header
freopen <stdio.h><stdio.h>
_wfreopen <stdio.h> 或 <wchar.h><stdio.h> or <wchar.h>

通用 Windows 平台 (UWP) 应用中不支持控制台。The console is not supported in Universal Windows Platform (UWP) apps. 与控制台关联的标准流句柄-stdinstdout,和stderr-必须将重定向,然后 C 运行时函数可以在 UWP 应用中使用它们。The standard stream handles that are associated with the console—stdin, stdout, and stderr—must be redirected before C run-time functions can use them in UWP apps. 有关其他兼容性信息,请参阅 兼容性For additional compatibility information, see Compatibility.


// 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" );  
      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'  

请参阅See Also

流 I/O Stream I/O
fclose、_fcloseall fclose, _fcloseall
_fdopen、_wfdopen _fdopen, _wfdopen
_fileno _fileno
fopen、_wfopen fopen, _wfopen
_open、_wopen _open, _wopen