Showing posts with label open pipe by using popen() function. Show all posts
Showing posts with label open pipe by using popen() function. Show all posts

Monday, July 4, 2011

popen() function example

// popen()  function to open pipe

# include <sys/wait.h>
# include <stdio.h>
# include <stdlib.h>
# define MAXLINE 4096

# define PAGER "${PAGER:-more}"

int main(int argc,char*argv[])
{
char line[MAXLINE];
FILE *fpin,*fpout;
if(argc!=2)
{
perror("\n usage :- a.out <path name>");
exit(0);
}
if((fpin=fopen(argv[1],"r"))==NULL)
{
printf("\nUnable to open %s",argv[1]);
}
if((fpout=popen(PAGER,"w"))==NULL)
perror("popen error...");

while(fgets(line,MAXLINE,fpin))
{
if(fputs(line,fpout)==EOF)
perror("\n fput error...");
}

if(ferror(fpin))
perror("fgets error...");
if(pclose(fpout)==-1)
perror("pclose error..");
exit(0);
}