I have an interactive program which loops until input is '1'.
It asks for user input and prints it.
#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;
}
It works well as long as I type in a string and press enter.
However, when I open another shell windows and echo into its /proc/<pid>/fd/0
.
It reads the input (I can see as if I typed the input) but it does not proceed to the output part.
When I echo I tried different inputs, all show same behavior:
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
Even when I bring the program terminal as the active shell, it does nothing.
Any idea how to make the interactive program behave as if I typed in the terminal itself?
Thanks
When you run :
echo hello > /proc/pid/fd/0
You didn't feed hello
into the standard input of the program, you feed it into the destination of the standard input, which is the terminal, reason why you saw it on the terminal.
A workaround would be to start your program with a pipe :
cat | ./your-cpp-program