且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

尽管替换了标准输入,为什么我的管道没有在 printf 中读取?

更新时间:2023-02-23 12:41:35

您使用 printf(5"); 将数据中继到子进程,但标准输出至少是行缓冲的所以在发送换行符之前不会写入任何内容(或 fflush(stdout)).然后你 close(1); 关闭标准输出,它不会刷新标准 I/O 缓冲区.所以什么都没有发送给孩子.使用 fclose(stdout); 将 5 发送给孩子.

You use printf("5"); to relay the data to the child, but standard output is at least line-buffered so nothing is written until a newline is sent (or fflush(stdout)). Then you close(1); to close standard output, which does not flush the standard I/O buffers. So nothing is sent to the child. Use fclose(stdout); to get the 5 sent to the child.