I'm using pipes to generate a child process which reads a part from a file and send it through the pipe so the parent process can read it, and execute an extern program and redirect the stdout to a file.
The problem is that the stdout of the program that executes execv is not saved in my temporalFile.
1- Am I wrong using execv??(it I used before system function but the professor said that it was not performace was bad)
2- can be handle the stdout from execv ?
3-is there another alternative to execute an extern program passing parameters to the extern program and capture its stdout?
4-am I missing something in my code?
5-if I do a print, the stdout is capture by the temporalFile.
pd:I'm sorry my english is very bad.
int main()
{
int fd[2];
pid_t childpid;
char readbuffer[80];
pipe(fd);
if((childpid = fork()) == -1){
perror("error at fork");
exit(1);
}
if(childpid == 0){
/* Child process closes up input side of pipe */
close(fd[0]);
FILE* archEntry=fopen("datosDeEntrada.txt","r");
char string[3];
fseek(archEntry,4,SEEK_SET);
fread(string,3,1,archEntry);
/* Send "string" through the output side of pipe */
write(fd[1], string, (strlen(string)+1));
exit(0);
}
else{
/* Parent process closes up output side of pipe */
close(fd[1]);
FILE* fdArch=fopen("temporalFile.txt","w");
dup2(fileno(fdArch),STDOUT_FILENO);
/* Read in a string from the pipe */
read(fd[0], readbuffer, sizeof(readbuffer));
char *argv[]={ "workspace/project/Debug/./externProgram ", readbuffer, NULL};
execv(argv[0],argv);
fclose(fdArch);
}
return 0;
}