If you do not have a file named newin that you want to use, your freopen call will fail. (In addition, you should not close the stream before calling freopen , since freopen expects a valid stream, which it closes as a side effect.) To reopen standard input for reading from the terminal, you need to specify /dev/tty as the file name:
if (!freopen("/dev/tty", "r", stdin)) { perror("/dev/tty"); exit(1); }
If the program is under your control, there is no reason to reopen stdin to start - just open /dev/tty as a separate file pointer and use it to get interactive input:
FILE *tty = fopen("/dev/tty", "r"); if (!tty) { perror("/dev/tty"); exit(1); } ... while (1) { fgets(c, sizeof(c), tty); ... }
In any case, it is not possible to automate interactive responses using shell pipes, but it looks like what you want. Reading from /dev/tty is standard practice in command programs that support both input redirection and interactive work, for example, some Unix editors.
user4815162342
source share