Chinaunix首页 | 论坛 | 博客
  • 博客访问: 208720
  • 博文数量: 69
  • 博客积分: 153
  • 博客等级: 入伍新兵
  • 技术积分: 595
  • 用 户 组: 普通用户
  • 注册时间: 2011-07-27 09:27
文章分类

全部博文(69)

文章存档

2012年(4)

2010年(13)

2009年(7)

2008年(45)

我的朋友

分类: 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没有被输出。

 

 

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