#define STR "xiamanman\n"
#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\n");

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

dup2()函数的使用,的更多相关文章

  1. [APUE]不用fcntl实现dup2函数功能

    dup2的函数定义为: #include <unistd.h> int dup2(int src_fd, int new_fd); 自己实现dup2函数有几个关键点: 1,检查给定的源fd ...

  2. dup和dup2函数

    下面两个函数都可用来复制一个现存的文件描述符: #include<unistd.h> int dup(int filedes); int dup2(int filedes,int file ...

  3. dup和dup2函数以及管道的实现

    疑问:管道应该不是这样实现的,因为这要求修改程序的代码 dup和dup2也是两个非常有用的调用,它们的作用都是用来复制一个文件的描述符.它们经常用来重定向进程的stdin.stdout和stderr. ...

  4. dup,dup2函数【转】

    转自:http://eriol.iteye.com/blog/1180624 转自:http://www.cnblogs.com/jht/archive/2006/04/04/366086.html ...

  5. Unix 网络编程 dup和dup2函数

    dup和dup2也是两个很实用的调用,它们的作用都是用来复制一个文件的描写叙述符. 它们经经常使用来重定向进程的stdin.stdout和stderr.这两个函数的原形例如以下: #include & ...

  6. linux之dup和dup2函数解析

    1. 文件描述符在内核中数据结构在具体说dup/dup2之前,我认为有必要先了解一下文件描述符在内核中的形态.一个进程在此存在期间,会有一些文件被打开,从而会返回一些文件描述符,从shell中运行一个 ...

  7. dup和dup2函数简单使用

    dup函数 头文件和函数原型: #include <unistd.h> int dup(int oldfd); dup函数是用来打开一个新的文件描述符,指向和oldfd同一个文件,共享文件 ...

  8. 文件I/O(不带缓冲)之dup和dup2函数

    下面两个函数都可用来复制一个现有的文件描述符: #include <unistd.h> int dup( int filedes ); int dup2( int filedes, int ...

  9. 详解linux进程间通信-管道 popen函数 dup2函数

    前言:进程之间交换信息的唯一方法是经由f o r k或e x e c传送打开文件,或通过文件系统.本章将说明进程之间相互通信的其他技术-I P C(InterProcess Communication ...

  10. dup与dup2函数

    依赖的头文件 #include <unistd.h> 函数定义 int dup(int oldfd); int dup2(int oldfd, int newfd); 函数作用 dup和d ...

随机推荐

  1. Android相对布局实例

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools= ...

  2. 安装qt5.3.2后,qtcreator在ubuntu 11.04无法启动的问题

    在官方网站下载.run文件安装后,qtcreator启动失败,然后找到命令行启动,失败原因如下: shr@shr-Sieyuan:~/Qt5.3.2/Tools/QtCreator/bin$ ./qt ...

  3. mysql多实例部署

    mysql 多实例常规来讲,主要有二种方案可以实现,这二种方案各有利弊,如下:1.基于多配置文件 通过使用多个配置文件来启动不同的进程,以此来实现多实例. 优点:逻辑简单,配置简单 缺点:管理起来不方 ...

  4. MySQL 获得当前日期时间 函数

    获得当前日期+时间(date + time)函数:now() mysql> select now(); +---------------------+ | now() | +---------- ...

  5. Maven之Nexus构建企业级Maven仓库

    什么是Nexus? Nexus是Maven仓库管理器,用来搭建一个本地仓库服务器,这样做的好处是便于管理,节省网络资源,速度快,还有一个非常有用的功能就是可以通过项目的SNAPSHOT版本管理,来进行 ...

  6. Word and MediaElement

    function jmc_new_lib(){// Because we still want the script to load but not the styleswp_enqueue_scri ...

  7. Python 编码规范

    官网规范:https://www.python.org/dev/peps/pep-0008/ 1.不在同一句import中引用多个库 # 正确姿势: import os import sys # 错误 ...

  8. oracle 存储过程和函数例子

    关于 游标 if,for 的例子 create or replace procedure peace_if is cursor var_c is select * from grade; begin ...

  9. andriod 新建Activity_ Form

    在一个Android工程,如何新建一个Activity? 一:新建一个类(*.class),继承自android.app.Activity类. 二:在res/layout目录下新建一个布局xml文件, ...

  10. https协议操作

    在伪静态中加入下列代码 <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{SERVER_PORT} 80 RewriteRul ...