#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. C语言初学者代码中的常见错误与瑕疵(9)

    题目 字母的个数 现在给你一个由小写字母组成字符串,要你找出字符串中出现次数最多的字母,如果出现次数最多字母有多个那么输出最小的那个. 输入:第一行输入一个正整数T(0<T<25) 随后T ...

  2. mysql相关总结

    mysql设置初始密码和更改密码(ZIP文件解压安装): http://blog.csdn.net/stypace/article/details/38232393

  3. https笔记

    TCP提供了可靠的,面向连接的字节流服务. 1)应用数据分割成TCP认为适合发送的数据块,通过MSS(最大数据包长度)来控制. 2)重传机制 3)对首部和数据进行校验 4)TCP对收到的数据进行排序, ...

  4. bash环境变量读取顺序

    bash环境变量读取顺序: 交互式登录的用户: /etc/profile --> /etc/profile.d/*.sh --> ~/.bash_profile --> ~/.bas ...

  5. 【PHP设计模式 04_GongChang.php】 工厂方法

    <?php /** * [工厂方法] * 之前 03.php 简单工厂,如果再增加一个oracle客户端,就需要再次修改服务端Factory的代码. * 在面向对象设计法则中,有一个重要的[开闭 ...

  6. http://www.cnblogs.com/xqin/p/4862849.html

    一.前言 半年前左右折腾了一个前后端分离的架子,这几天才想起来翻出来分享给大家.关于前后端分离这个话题大家也谈了很久了,希望我这个实践能对大家有点点帮助,演示和源码都贴在后面. 二.技术架构 这两年a ...

  7. js正则匹配

    var account = $('input[name="account"').val(); var re = /^[0-9]+.?[0-9]*$/; if (!re.test(a ...

  8. POJ 2001:Shortest Prefixes

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 16782   Accepted: 728 ...

  9. c# TCPclient

    服务端 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; ...

  10. Unity ScriptableObject的使用

    ScriptableObject主要实现对象序列化的保存,因为是Unity自己的序列化,所以比xml,json序列化方便很多,但相对可控性也比较差 1.Editor下写入和读取测试: using Un ...