Hello,
I am writing a program which calls an external program and obtains its output(standard output). I implemented this functionality as a method. When I called the method for the first time it successfully obtains the output, however, when I called multiple times in the same main() it does not obtain the output of the external program except the first call (i.e. I cant read-in the output of the secınd call and the following ones)
Could anyone please help me.
engin
Below is the main structure of the method:
...::ally(){
allyString.clear();//to collect the output of external program
pid_t pid;
int commpipe[2];
int status;
if(pipe(commpipe)==-1){
cout<<"Error in opening pipe"<<endl;
exit(1);
} pid=fork();
if(pid==-1){
cout<<"Error in forking"<<endl;
exit(1);
}
if(pid>0){//parent process close(commpipe[1]);
dup2(commpipe[0],0);//replace stdin with read side of pipe
wait(&status);//wait for the child to finish
while(!cin.eof()){
char c=cin.get();
allyString.push_back(c);
} close(commpipe[0]);
}else{//child process close(commpipe[0]);
dup2(commpipe[1],1);//replace stdout with write side of pipe
if(execl("/src/local/bin/ally", "ally","154.54.20.26", "154.54.20.26",(char *)0)==-1){//the external program
cout<<"Error in executing the external ally program "<<endl;
exit(1);
}
cout<<"Finished Child process";
close(commpipe[1]);
}
}