_fdopen、_wfdopen_fdopen, _wfdopen

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

语法Syntax

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

参数Parameters

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

模式mode
文件访问的类型。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.

备注Remarks

_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 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对返回文件* 也会关闭文件描述符。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. 通过使用可重新定位文件指针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"). 但是,在读取与写入之间切换时,必须有干预性fflushfsetposfseek,或者rewind操作。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.

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
aa _O_WRONLY | _O_追加(通常 _O_WRONLY | _O_创建| _O_追加)_O_WRONLY | _O_APPEND (usually _O_WRONLY | _O_CREAT | _O_APPEND)
+a+ _O_RDWR | _O_追加(通常 _O_RDWR | _O_追加| _O_创建)_O_RDWR | _O_APPEND (usually _O_RDWR | _O_APPEND | _O_CREAT )
rr _O_RDONLY_O_RDONLY
r +r+ _O_RDWR_O_RDWR
ww _O_WRONLY (通常 _O_WRONLY | _O_创建| _O_TRUNC)_O_WRONLY (usually _O_WRONLY | _O_CREAT | _O_TRUNC)
w +w+ _O_RDWR (通常 _O_RDWR | _O_创建| _O_TRUNC)_O_RDWR (usually _O_RDWR | _O_CREAT | _O_TRUNC)
bb _O_BINARY_O_BINARY
tt _O_TEXT_O_TEXT
cc None
nn None

要求Requirements

函数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.

示例Example

// 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 )
      count++;

   // 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

输出Output

Lines in file: 2

请参阅See also

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