_fdopen、_wfdopen_fdopen, _wfdopen

将流与以前为低级别 I/O 而打开的文件相关联。Associates a stream with a file that was previously opened for low-level I/O.


FILE *_fdopen(
   int fd,
   const char *mode
FILE *_wfdopen(
   int fd,
   const wchar_t *mode


打开文件的文件描述符。File descriptor of the open file.

文件访问的类型。Type of file access.

返回值Return Value

这些函数均返回指向打开流的指针。Each of these functions returns a pointer to the open stream. 一个 null 指针值指示错误。A null pointer value indicates an error. 出现错误时,会调用无效参数处理程序,如参数验证中所述。When an error occurs, the invalid parameter handler is invoked, as described in Parameter Validation. 如果允许执行继续,则将errno设置为ebadf ( ,这指示错误的文件描述符或EINVAL,这表示模式为空指针。If execution is allowed to continue, errno is set either to EBADF, which indicates a bad file descriptor, or EINVAL, which indicates that mode was a null pointer.

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


_Fdopen函数将 i/o 流与由fd标识的文件相关联,从而允许对为低级别 i/o 而打开的文件进行缓冲和格式化。The _fdopen function associates an I/O stream with the file that is identified by fd, and thus allows a file that is opened for low-level I/O to be buffered and formatted. _wfdopen_fdopen的宽字符版本; _wfdopenmode参数是宽字符字符串。_wfdopen is a wide-character version of _fdopen; the mode argument to _wfdopen is a wide-character string. _wfdopen_fdopen的行为方式相同。_wfdopen and _fdopen otherwise behave identically.

传递给 _fdopen的文件描述符由返回的文件* 流拥有。File descriptors passed into _fdopen are owned by the returned FILE * stream. 如果 _fdopen成功,则不要在文件描述符上调用 _closeIf _fdopen is successful, do not call _close on the file descriptor. 对返回的文件* 调用fclose还会关闭文件说明符。Calling fclose on the returned FILE * also closes the file descriptor.

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

Tchar.h 例程Tchar.h routine _未定义_UNICODE 和 MBCS_UNICODE and _MBCS not defined _定义 MBCS_MBCS defined _已定义 UNICODE_UNICODE defined
_tfdopen_tfdopen _fdopen_fdopen _fdopen_fdopen _wfdopen_wfdopen

模式字符串指定为文件请求的文件访问类型:The mode character string specifies the type of file access requested for the file:

模式mode AccessAccess
“r”"r" 打开以便读取。Opens for reading. 如果文件不存在或找不到, 则fopen调用失败。If the file does not exist or cannot be found, the fopen call fails.
“w”"w" 打开用于写入的空文件。Opens an empty file for writing. 如果给定文件存在,则其内容会被销毁。If the given file exists, its contents are destroyed.
“a”"a" 打开以在文件末尾进行写入(追加)。Opens for writing at the end of the file (appending). 创建文件(如果文件不存在)。Creates the file if it does not exist.
“r+”"r+" 打开以便读取和写入。Opens for both reading and writing. 文件必须存在。The file must exist.
“w+”"w+" 打开用于读取和写入的空文件。Opens an empty file for both reading and writing. 如果文件存在,则其内容会被销毁。If the file exists, its contents are destroyed.
“a+”"a+" 打开以进行读取和追加。Opens for reading and appending. 创建文件(如果文件不存在)。Creates the file if it does not exist.

使用 "a""a +" 访问类型打开文件时,所有写入操作都将在文件末尾进行。When a file is opened with the "a" or "a+" access type, all write operations occur at the end of the file. 可以通过使用fseek倒带重定位文件指针,但在执行任何写入操作之前,该指针始终会移回文件末尾。因此,无法覆盖现有数据。The file pointer can be repositioned by using fseek or rewind, but it is always moved back to the end of the file before any write operation is carried out. Thus, existing data cannot be overwritten. 指定 "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"). 但是,在读取和写入之间切换时,必须有干预fflushfsetposfseek倒带操作。However, when you switch between reading and writing, there must be an intervening fflush, fsetpos, fseek, or rewind operation. 如果需要,可以为fsetposfseek操作指定当前位置。You can specify the current position for the fsetpos or fseek operation, if you want to.

除了以上值之外,还可以在模式中包含以下字符以指定换行符的转换模式:In addition to the above values, the following characters can also be included in mode to specify the translation mode for newline characters:

模式修饰符mode modifier 行为Behavior
tt 在文本(转换)模式下打开。Open in text (translated) mode. 在这种模式下,输入时,回车换行 (CR-LF) 组合将转换为单一的换行 (LF);输出时,LF 字符将转换为 CR-LF 组合。In this mode, carriage return-line feed (CR-LF) combinations are translated into one-line feeds (LF) on input, and 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.
bb 在二进制(未转换)模式下打开。Open in binary (untranslated) mode. 禁止任何来自t模式的翻译。Any translations from t mode are suppressed.
cc 启用关联文件名的提交标志,以便在调用fflush_flushall时,将文件缓冲区的内容直接写入磁盘。Enable the commit flag for the associated filename so that the contents of the file buffer are written directly to disk if either fflush or _flushall is called.
nn 将关联的文件名的提交标志重置为 "无提交"。Reset the commit flag for the associated filename to "no-commit." 这是默认设置。This is the default. 如果将程序显式链接到 Commode.obj,它还将重写全局提交标志。除非将程序显式链接到 Commode.obj,全局提交标志默认为“no-commit”。It also overrides the global commit flag if you link your program with Commode.obj. The global commit flag default is "no-commit" unless you explicitly link your program with Commode.obj.

Tcn 模式选项是 Microsoft fopen_fdopen的扩展。The t, c, and n mode options are Microsoft extensions for fopen and _fdopen. 如果要保留 ANSI 可移植性,请不要使用它们。Do not use them if you want to preserve ANSI portability.

如果在mode中未给出tb ,则默认转换模式由全局变量 _fmode定义。If t or b is not given in mode, the default translation mode is defined by the global variable _fmode. 如果tb作为参数的前缀,则函数将失败并返回 NULL。If 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.

Fopen_fdopen中使用的模式字符串的有效字符对应于 _open _sopen中使用的oflag参数,如下表所示:Valid characters for the mode string used in fopen and _fdopen correspond to oflag arguments used in _open and _sopen, as shown in this table:

模式字符串中的字符Characters in mode string _Open_sopen的等效oflagEquivalent oflag value for _open and _sopen
aa _O_ | WRONLYoAPPEND_(通常为 o WRONLY o _ | __ __ | __追加_O_WRONLY | _O_APPEND (usually _O_WRONLY | _O_CREAT | _O_APPEND)
a+a+ _O_ | RDWRoappend_(通常为 o RDWR o append o _ | __ | ___ _ )_O_RDWR | _O_APPEND (usually _O_RDWR | _O_APPEND | _O_CREAT )
ww _O_WRONLY | ( _WRONLY | o TRUNC )__ ____O_WRONLY (usually _O_WRONLY | _O_CREAT | _O_TRUNC)
w+w+ _O_RDWR | ( _RDWR | o TRUNC )__ ____O_RDWR (usually _O_RDWR | _O_CREAT | _O_TRUNC)
cc None
nn None


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

有关更多兼容性信息,请参阅 兼容性For more compatibility information, see Compatibility.


// crt_fdopen.c
// This program opens a file by using low-level
// I/O, then uses _fdopen to switch to stream
// access. It counts the lines in the file.

#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
#include <share.h>

int main( void )
   FILE *stream;
   int  fd, count = 0;
   char inbuf[128];

   // Open a file.
   if( _sopen_s( &fd, "crt_fdopen.txt", _O_RDONLY, _SH_DENYNO, 0 ) )
      exit( 1 );

   // Get stream from file descriptor.
   if( (stream = _fdopen( fd, "r" )) == NULL )
      exit( 1 );

   while( fgets( inbuf, 128, stream ) != NULL )

   // After _fdopen, close by using fclose, not _close.
   fclose( stream );
   printf( "Lines in file: %d\n", count );

输入:crt_fdopen.txtInput: crt_fdopen.txt

Line one
Line two


Lines in file: 2

请参阅See also

流 I/OStream I/O
_dup、 _dup2_dup, _dup2
fclose、 _fcloseallfclose, _fcloseall
fopen、 _wfopenfopen, _wfopen
freopen、 _wfreopenfreopen, _wfreopen
_open、 _wopen_open, _wopen