zoukankan      html  css  js  c++  java
  • dup2()函数的使用,

    #define STR "xiamanman "
    #define STR_LEN 10
    #define STDOUT 1

    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>


    int main()
    {
      int fd = open("./temp", O_CREAT|O_RDWR|O_APPEND);

      int s_fd = dup(STDOUT);

      dup2(fd, STDOUT);

      close(fd);

      write(STDOUT, STR, STR_LEN);

      dup2(s_fd, STDOUT);

      close(s_fd);

      write(STDOUT, STR, STR_LEN);

      printf("xx ");

      return 0;
    }

    --------------------------------------------------------------------

    参考资料,dup2 百度百科。主要参考英文的代码。

    #define STDOUT 1

    int main(void)
    {

      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;
    }

  • 相关阅读:
    DELPHI中Showmodal与Show的区别
    怎样把一个文件夹里面所有文件的文件名提取出来,放到Excel表格里呢
    Python基础之函数
    Python_Openpyxl
    设计模式
    【转】深入理解递归函数的调用过程
    Java集合的Stack、Queue、Map的遍历
    数据结构-String、char
    leetcode-位运算
    Java web入门
  • 原文地址:https://www.cnblogs.com/zhangxuan/p/5970633.html
Copyright © 2011-2022 走看看