//运行环境:ubuntu9
//可以实现对管道的多次读取
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main()
{
FILE *read_fp;
char buffer[BUFSIZ+1];
int chars_read;
memset(buffer,'\0',sizeof(buffer));
read_fp=popen("ps -al","r");
if(read_fp!=NULL){
chars_read=fread(buffer,sizeof(char),BUFSIZ,read_fp);
while(chars_read>0) {
buffer[chars_read-1]='\0';
printf("Reading %d :-\n%s\n",BUFSIZ,buffer);
chars_read=fread(buffer,sizeof(char),BUFSIZ,read_fp);
}
pclose(read_fp);
exit(EXIT_SUCCESS);
}
exit(EXIT_FAILURE);
}