dup与dup2

系统编程(三)文件IO之常用API dup dup2

今天我们介绍经常用的两个函数,这两个函数在做读写重定向和文件共享的时候是非常有用的他们就是dup和dup2

int dup( int oldfd );  
int dup2( int oldfd, int targetfd );  
 
 
  • 1
  • 2

oldfd:要被复制的文件描述符
newfd:在dup2函数中指定的新文件描述符
返回值:调用成功返回新的文件描述符,调用失败返回 -1

首先我们来先看下dup的用法,一般我们打开相同的文件的时候我们的读写指针不是共享的,但是使用dup之后可以共享读写指针。

#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define file "demo3.txt"
int main(int argc, char *argv[])
{
    int fd1, fd2, fd3;

    char cmd[] = "cat " file "; echo";

    fd1 = open(file, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
    if (fd1 == -1)
    {
        printf("open file2 failed\n");
    }

    fd2 = dup(fd1);             //共享fd1的读写指针
    if (fd2 == -1)
    {
        printf("open file2 failed\n");
    }
    fd3 = open(file, O_RDWR);
    if (fd3 == -1)
    {
        printf("open file2 failed\n");
    }
    printf("%d   %d   %d\n",fd1,fd2,fd3 );

    if (write(fd1, "Hello,", 6) == -1)
    {
        printf("write file1 failed\n");
    }
    system(cmd);

    if (write(fd2, " world", 6) == -1)
    {
        printf("write file2 failed\n");
    }
    system(cmd);

    if (write(fd3, "Gidday", 6) == -1)
    {
        printf("write file3 failed\n");
    }
    system(cmd);

    close(fd1);
    close(fd2);
    close(fd3);
    return 0;
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51

结果:

3   4   5
Hello,
Hello, world
Gidday world
 
 
  • 1
  • 2
  • 3
  • 4

dup2一般用于重定向文件操作

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

int main()
{
      int fd1,fd2;
      fd1 = dup(STDOUT_FILENO);   //用fd1保存标准输出

      fd2 = open("demo3.txt",O_CREAT | O_RDWR | O_APPEND);
      if (-1 == fd2)
      {
          printf("open file error.\n");
          return -1;
      }

      if (-1 == dup2(fd2,STDOUT_FILENO) ) {//将fd2映射到标准输出
          printf("dup2 file error.\n");
          return -1;
      }

      write(STDOUT_FILENO,"hello fd2\n",10);//这里写入标准输出,会输入的文件里
      write(fd2,"hello Im fd2\n",15);//这也是项文件里写入

      if (-1 != dup2(fd1,STDOUT_FILENO) ) {//将标准输入恢复成与原来的状态
          write(STDOUT_FILENO,"normal\n",7);
      }

      close(fd2);
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
            <link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-b6c3c6d139.css" rel="stylesheet">
                                            <div class="more-toolbox">
            <div class="left-toolbox">
                <ul class="toolbox-list">
                    
                    <li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#csdnc-thumbsup"></use>
                    </svg><span class="name">点赞</span>
                    <span class="count"></span>
                    </a></li>
                    <li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;popu_824&quot;}"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#icon-csdnc-Collection-G"></use>
                    </svg><span class="name">收藏</span></a></li>
                    <li class="tool-item tool-active is-share"><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;1582594662_002&quot;}"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#icon-csdnc-fenxiang"></use>
                    </svg>分享</a></li>
                    <!--打赏开始-->
                                            <!--打赏结束-->
                                            <li class="tool-item tool-more">
                        <a>
                        <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
                        </a>
                        <ul class="more-box">
                            <li class="item"><a class="article-report">文章举报</a></li>
                        </ul>
                    </li>
                                        </ul>
            </div>
                        </div>
        <div class="person-messagebox">
            <div class="left-message"><a href="https://blog.csdn.net/bin_zhang1">
                <img src="https://profile.csdnimg.cn/9/1/0/3_bin_zhang1" class="avatar_pic" username="bin_zhang1">
                                        <img src="https://g.csdnimg.cn/static/user-reg-year/2x/2.png" class="user-years">
                                </a></div>
            <div class="middle-message">
                                    <div class="title"><span class="tit"><a href="https://blog.csdn.net/bin_zhang1" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}" target="_blank">疯狂飙车的蜗牛</a></span>
                                        </div>
                <div class="text"><span>发布了34 篇原创文章</span> · <span>获赞 3</span> · <span>访问量 1万+</span></div>
            </div>
                            <div class="right-message">
                                        <a href="https://im.csdn.net/im/main.html?userName=bin_zhang1" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信
                    </a>
                                                        <a class="btn btn-sm  bt-button personal-watch" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}">关注</a>
                                </div>
                        </div>
                </div>
</article>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值