_dup、_dup2

创建打开文件 (_dup) 第二个文件描述符,或重新分配文件描述符 (_dup2)。

int _dup( 
   int fd 
);
int _dup2( 
   int fd1,
   int fd2 
);

参数

  • fd, fd1
    引用开启文件的描述符。

  • fd2
    任何文件描述符。

返回值

_dup 返回一个新文件描述符。 _dup2 返回 0 指示成功。 如果发生错误,每个函数返回– 1 并将 errno 设置到 EBADF,如果文件说明符无效或为 EMFILE,如果没有其他文件描述符不可用。 万一文件描述符无效,该函数调用无效参数处理程序,如 参数验证所述。

有关这些属性和其他的更多信息返回代码示例,请参见 _doserrno、errno、_sys_errlist 和 _sys_nerr

备注

_dup 和 _dup2 函数关联当前打开的文件第二个文件描述符。 这些函数可用于将预定义的文件描述符,如 stdout,不同的文件。 使用任何一个文件描述符,文件中的操作可执行。 允许的文件访问的类型不受创建一个新的描述符影响。 _dup 返回特定文件的下一个可用的文件描述符。 _dup2 强制 fd2 引用和 fd1相同的文件。 如果 fd2 在调用时与打开文件关联,该文件已关闭。

_dup 和 _dup2 都接受文件描述符作为参数。 若要通过流 (FILE *) 到其中一种函数,请使用 _fileno。 fileno 实例准确的返回与当前特定流联系的文件描述符。 下面的示例演示如何将 stderr (定义为 Stdio.h 的 FILE * ) 与文件描述符关联:

int cstderr = _dup( _fileno( stderr ));

要求

例程

必需的标头

_dup

<io.h>

_dup2

<io.h>

控制台在 Windows 应用商店 应用程序中不受支持。 与控制台 stdin、stdout 和 stderr 关联的标准流句柄必须重定向,然后 C 运行时函数才可以在 Windows 应用商店 应用程序中使用它们。 有关兼容性的更多信息,请参见兼容性

示例

// crt_dup.c
// This program uses the variable old to save
// the original stdout. It then opens a new file named
// DataFile and forces stdout to refer to it. Finally, it
// restores stdout to its original state.
//

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

int main( void )
{
   int old;
   FILE *DataFile;

   old = _dup( 1 );   // "old" now refers to "stdout" 
                      // Note:  file descriptor 1 == "stdout" 
   if( old == -1 )
   {
      perror( "_dup( 1 ) failure" );
      exit( 1 );
   }
   _write( old, "This goes to stdout first\n", 26 );
   if( fopen_s( &DataFile, "data", "w" ) != 0 )
   {
      puts( "Can't open file 'data'\n" );
      exit( 1 );
   }

   // stdout now refers to file "data" 
   if( -1 == _dup2( _fileno( DataFile ), 1 ) )
   {
      perror( "Can't _dup2 stdout" );
      exit( 1 );
   }
   puts( "This goes to file 'data'\n" );

   // Flush stdout stream buffer so it goes to correct file 
   fflush( stdout );
   fclose( DataFile );

   // Restore original stdout 
   _dup2( old, 1 );
   puts( "This goes to stdout\n" );
   puts( "The file 'data' contains:" );
   _flushall();
   system( "type data" );
}
  

请参见

参考

低级别 I/O

_close

_creat、_wcreat

_open、_wopen