我正在使用管道生成一个子进程,它从文件中读取一部分并通过管道发送它,以便父进程可以读取它,并执行外部程序并将标准输出重定向到文件。
问题是执行 execv 的程序的标准输出没有保存在我的 temporalFile 中。
1-我使用execv错了吗??(我在系统功能之前使用过,但教授说它不是性能不好)
2-可以处理来自 execv 的标准输出吗?
3-是否有另一种选择来执行将参数传递给外部程序并捕获其标准输出的外部程序?
4-我的代码中是否缺少某些内容?
5-如果我进行打印,则标准输出由 temporalFile 捕获。
pd:对不起,我的英语很糟糕。
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;
}