该程序读取文件的内容并通过管道发送。我在阅读子进程中的管道时遇到问题。输出文件中的内容为方形字符。我想我必须将c从地址改为值?
if (pid > 0) { /* parent */
/* close the end of the pipe we do not need */
close(pfd[0]);
/* read from the input file and write to the pipe */
while ((c = getc(from)) != EOF){
if (flipping){
c = flipChar(c);
write(pfd[1],&c, 1);
}
else
write(pfd[1],&c, 1);
}
fclose(from);
close(pfd[1]);
wait(NULL);
}
else{ /* child process */
close(pfd[1]);
while (c = read(pfd[0],&c,1))
{
/* change c from a address to value?? */
putc(c, destfile);
}
fclose(destfile);
close(pfd[0]);
}
return 0;
}
答案 0 :(得分:0)
read()
返回读取的字符数。所以你执行read(pfd[0], &c, 1)
,它将一个字符存储在c
中,然后立即用值1覆盖该字符(因为你从管道中读取了一个字符)。