提问人:Pete Mitchell 提问时间:9/3/2021 更新时间:9/3/2021 访问量:545
如何在 C 中读取 stdin 然后键盘输入?
How to read stdin then keyboard input in C?
问:
我正在尝试读取重定向/通过管道传输到我的程序的内容(例如程序<测试.txt),然后显示命令提示符并读取键盘输入。
管道输入正确打印,然后在 stdin 中引发 EOF。在那之后,我似乎无法获得键盘输入。例如,以下代码无限循环:它打印的字符为 -1 (EOF)。
您有什么建议来让键盘输入正常工作?
#include <stdio.h>
#include <unistd.h>
int main()
{
int ch;
while (EOF != (ch = getchar())) {
printf("%c", ch);
}
sleep(3);
while (1) {
while ('\n' != (ch = getchar())) {
printf("%c", ch);
}
}
return 0;
}
答: 暂无答案
评论
./command -f config
/dev/tty