我在读取两个有两个孩子的两个输入文件时遇到了问题。两个孩子应该从每个文件中交替读取一个单词,将它们发送给父级,父级将像这样输出它们:
输入1:“我真的”
输入2:“我很愚蠢”
输出:“我真的很愚蠢”
我如何使用函数打开(用于打开输入文件进行读取)和读取来做到这一点?孩子的“儿子”功能(孩子会有两个类似的功能,一个读取偶数单词,一个读取奇数单词)应该是这样的:
void son(char i_file, int p[2], int bro_pid){
int c,fd=open("i_file", O_RDONLY, S_IWUSR);
char buf;
if(fd > 0){
close(p[0]);
while(c=read(i_file, buf, 1) == 1){ //Here should be a conditions to continue reading till EOF
.... //I need your help here
....
write(p[1], buf, c); //Here i write in the pipe the word for the parent
while(1){
pause(); //The child will wait a signal from the parent before reading the next word
break;
}
}
}else
perror("Error opening file secondo figlio");
close(fd);
close(p[1]);
}
你能帮我解决这个问题吗?在此先感谢您的帮助。
编辑:我误读了练习,有两个不同的输入文件。