Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5705168
  • 博文数量: 675
  • 博客积分: 20301
  • 博客等级: 上将
  • 技术积分: 7671
  • 用 户 组: 普通用户
  • 注册时间: 2005-12-31 16:15
文章分类

全部博文(675)

文章存档

2012年(1)

2011年(20)

2010年(14)

2009年(63)

2008年(118)

2007年(141)

2006年(318)

分类: C/C++

2006-11-16 18:09:32

#include
#include
#include
#include
#include

int main()
{
    int fd1;

    fd1 = open("foo.txt",O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR);
    dup2(fd1,1);
    system("ls");

    return 0;
}

dup2函数是拷贝文件描述符,上面程序的主要的目的是将标准输出重定向到一个文件里面。刚开始的时候,对dup的函数描述不是很理解,不过现在告清楚了。

wangyao@fisherman:~/Desktop/temp/dup$ make
gcc -o stdout2fd stdout2fd.c
wangyao@fisherman:~/Desktop/temp/dup$ ls
foo.txt  makefile  stdout2fd  stdout2fd.c
wangyao@fisherman:~/Desktop/temp/dup$ cat foo.txt
foo.txt
makefile
stdout2fd
stdout2fd.c
wangyao@fisherman:~/Desktop/temp/dup$

可以看到程序的程序的运行情况了

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