Chinaunix首页 | 论坛 | 博客
  • 博客访问: 230251
  • 博文数量: 131
  • 博客积分: 259
  • 博客等级: 二等列兵
  • 技术积分: 705
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-21 21:15
文章分类

全部博文(131)

文章存档

2013年(3)

2011年(128)

分类:

2011-04-18 21:41:58

原文地址:printf重定向 作者:checl1987_EE

chechunli@chechunli-PC:tmp $ cat redirect.c
#include
#include
#include
#include
#include

#define MAX_SIZE             1024
#define OPENFILE             "1"

int main(int argc, char *argv[])
{
    int n;
    int fd[2];
    pid_t pid;
    char line[MAX_SIZE];
int fd_test;

    if (pipe(fd) < 0)
        perror("pipe error");
    if ((pid = fork()) < 0) {
        perror("fork error");
    } else if (pid > 0) {
        close(fd[0]); 
close(STDOUT_FILENO);
dup2(fd[1], STDOUT_FILENO);
system("ls -l");
system("ls");
    } else {
        close(fd[1]);
fd_test = open(OPENFILE, O_RDWR | O_CREAT, 0777);
while((n = read(fd[0], line, MAX_SIZE)) > 0)
if(write(fd_test, line, n) != n)
perror("write error");
    }
close(fd_test);

    return 0;
}
chechunli@chechunli-PC:tmp $ gcc redirect.c -Wall -o redirect
阅读(1004) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~