dup2

dup2

中文名 dup2
名词领域 编程
目录导航

函数

         函数名: dup2

  功 能: 复制文件句柄

  用 法: int dup2(int oldhandle, int newhandle);

程序例子

  #include <sys\stat.h>

  #include <string.h>

  #include <fcntl.h>

  #include <io.h>

  int main(void)

  {

  #define STDOUT 1

  int nul, oldstdout;

  char msg[] = "This is a test";

  /* create a file */

  nul = open("DUMMY.FIL", O_CREAT | O_RDWR,

  S_IREAD | S_IWRITE);

  /* create a duplicate handle for standard

  output */

  oldstdout = dup(STDOUT);

  /*

  redirect standard output to DUMMY.FIL

  by duplicating the file handle onto the

  file handle for standard output.

  */

  dup2(nul, STDOUT);

  /* close the handle for DUMMY.FIL */

  close(nul);

  /* will be redirected into DUMMY.FIL */

  write(STDOUT, msg, strlen(msg));

  /* restore original standard output

  handle */

  dup2(oldstdout, STDOUT);

  /* close duplicate handle for STDOUT */

  close(oldstdout);

  return 0;

  }

相关百科
返回顶部
产品求购 求购