【发布时间】:2019-05-24 06:54:48
【问题描述】:
我正在尝试用另一个管道替换标准输入,然后将原始标准输入放回 fd #0。
例如
dup2(p, 0); // p is a pre-existing fd of a pipe
exec(/* some commands */);
//what will be here in order to have the original stdin back?
scanf(...) //continue processing with original stdin.
【问题讨论】:
-
简单地将
dup2()stdin 输入到一个临时文件描述符,然后dup2()稍后再返回 -
@Ctx 是否可以保留标准输入而不临时复制它?
-
不要“保留”它,如果可能的话,您可以重新打开它。但是,根据您的标准输入,这可能是不可能的。
-
“exec..()”命令不会返回,除非它们失败。所以 exec 命令后唯一合理的语句是:
perror( "exec.. failed" );后跟exit( EXIT_FAILURE );
标签: c unix file-descriptor dup2