Chinaunix首页 | 论坛 | 博客
  • 博客访问: 208648
  • 博文数量: 70
  • 博客积分: 2010
  • 博客等级: 大尉
  • 技术积分: 585
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-11 13:36
文章分类

全部博文(70)

文章存档

2010年(17)

2009年(7)

2008年(46)

我的朋友
最近访客

分类: LINUX

2008-08-28 11:18:20

在linux下,关闭stdin,stdout,stderr后重新打开的方法:先用dup2(...)函数复制stdin,stdout,stderr文件描述符,然后复制回去。代码示例:
 

#include <stdio.h>
#include <unistd.h>

int main(int argc, char* argv[])
{
     //this integer variable must be a value between 0 to 255

    int fd = 100

    dup2( 1, fd );
    printf("copy 1 to fd\n");

    //close standard output
    close(1);
    printf("close 1\n");

    //reopen standard output
    dup2(fd, 1);
    printf("cope fd to 1\n");
    return 0;
}

 

输出结果:

copy 1 to fd
copy fd to 1

因为标准输出被close所以close 1没有被输出。

 

 

阅读(648) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~