_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(这指示 mode 是空指针)。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;模式参数_wfdopen是宽字符字符串。_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 *流。File descriptors passed into _fdopen are owned by the returned FILE * stream. 如果_fdopen成功,请不要调用_关闭上的文件描述符。If _fdopen is successful, do not call _close on the file descriptor. 调用fclose对返回FILE *也会关闭的文件描述符。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 _fdopen _fdopen _wfdopen

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

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

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

在文件末尾打开进行编写(追加)。Opens for writing, at the end of the file (appending). 创建文件(如果文件不存在)。Creates the file 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.

打开以进行读取和追加。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. 使用 fseekrewind 可重新定位文件指针,但在执行任何写入操作前,文件指针将始终被移回文件末尾。因此,无法覆盖现有数据。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"). 但是,在读取与写入之间切换时,必须有中间 fflushfsetposfseekrewind 操作。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:

在文本(转换)模式下打开。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. 在打开以进行读取/写入的文件中,fopen 将检查文件末尾的 Ctrl+Z 并在可能的情况下将其移除。In files opened for reading/writing, fopen checks for a Ctrl+Z at the end of the file and removes it, if possible. 这是因为使用 fseekftell 函数在以 CTRL+Z 结尾的文件中移动时,可能导致 fseek 在文件末尾附近错误运行。This is done because using the fseek and ftell functions to move within a file that ends with a Ctrl+Z might cause fseek to behave incorrectly near the end of the file.

在二进制(未转换)模式下打开。Open in binary (untranslated) mode. 会禁止从 t 模式进行任何转换。Any translations from t mode are suppressed.

启用关联 filename 的提交标志,以便在调用 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.

将关联 filename 的提交标志重置为“no-commit”。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.

tc,和n模式选项是 Microsoft 扩展fopen_fdopenThe 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.

如果tb中未给定模式,则默认转换模式由全局变量 _fmodeIf 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对应于oflag中使用自变量_打开_sopen,此表中所示: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 等效oflag_open_sopenEquivalent oflag value for _open and _sopen
a _O_WRONLY |_O_追加(通常 _O_WRONLY |_O_CREAT |_O_追加)_O_WRONLY | _O_APPEND (usually _O_WRONLY | _O_CREAT | _O_APPEND)
a+ _O_RDWR |_O_追加(通常 _O_RDWR |_O_追加 |_O_CREAT )_O_RDWR | _O_APPEND (usually _O_RDWR | _O_APPEND | _O_CREAT )
w+ _O_RDWR (通常 _O_RDWR |_O_CREAT |_O_TRUNC)_O_RDWR (usually _O_RDWR | _O_CREAT | _O_TRUNC)
c None
n None


函数Function 必需的标头Required header
_fdopen <stdio.h><stdio.h>
_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/O Stream I/O
_dup, _dup2 _dup, _dup2
fclose、 _fcloseall fclose, _fcloseall
fopen、 _wfopen fopen, _wfopen
freopen, _wfreopen freopen, _wfreopen
_打开, _wopen_open, _wopen