Chinaunix

标题: 请教: ls | wc >test.txt如何实现? ls | wc 程序已有 [打印本页]

作者: beck2000    时间: 2004-03-09 13:18
标题: 请教: ls | wc >test.txt如何实现? ls | wc 程序已有
下面的程序可执行: ls | wc
代码:
<code>;
#include <stdio.h>;

int main(void)
{
    int pfd[2];
    int pid;

    if (pipe(pfd) == -1)
    {
       perror("pipe failed";
       exit(1);
    }
    if ((pid = fork()) < 0)
    {
       perror("fork failed";
       exit(2);
    }
    if (pid == 0)
   {
       close(pfd[1]);
       dup2(pfd[0], 0);
       close(pfd[0]);
       execlp("wc", "wc",  (char *) 0);
       perror("wc failed";
       exit(3);
    } else {
       close(pfd[0]);
       dup2(pfd[1], 1);
       close(pfd[1]);
       execlp("ls", "ls",  (char *) 0);
       perror("ls failed";
       exit(4);
    }
    exit(0);
}

</code>;
作者: mzpvsww    时间: 2004-03-09 16:07
标题: 请教: ls | wc >test.txt如何实现? ls | wc 程序已有
写个shell不就都解决了 啊,还要这样写程序




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2