在shell里利用“|”管道干的事情就是io重定向,把“|”命令前的输出重定向到“|”后的标准输入中也就是c程序的stdin流中,所以要实现楼主所得功能程序只要跟原来的样就行了。例如
#include <stdio.h>
int main(void)
{
char string[512];
fgets(string ,512,stdin);
puts(string);
return 0;
}
--------------------------------------------------------------
example:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
int main(void)
{
int len ;
int apipe[2] ;
int i ;
char buf[256] ;
if (pipe(apipe) == -1) {
perror("can not create a pipe/n") ;