我有一个交互式程序,它循环直到输入为“1”。
它要求用户输入并打印出来。
#include <iostream>
#include <string>
int main() {
std::string input;
while (true) {
std::cout << "Enter input (enter 1 to exit): ";
std::cin >> input;
if (input == "1") {
break;
}
std::cout << "You entered: " << input << std::endl;
}
return 0;
}
只要我输入一个字符串并按下回车键,它就能正常工作。
但是,当我打开另一个 shell 窗口并回显其中的内容时/proc/<pid>/fd/0
,它会读取输入(我可以看到好像是我输入的输入),但它不会继续输出部分。
当我回应时,我尝试了不同的输入,都显示相同的行为:
echo 5 > /proc/pid/fd/0
echo hello > /proc/pid/fd/0
echo "hey\n" > /proc/pid/fd/0
echo 'hi\xA0' > /proc/pid/fd/0
即使我将程序终端作为活动 shell,它也不会执行任何操作。
有什么想法可以使交互式程序表现得像我在终端本身中输入的那样吗?
谢谢