I have a simple C++ program that uses cin to read eight numbers, and I'm running it in a bash shell. When I run it in the terminal, it looks like this (the user types in the numbers 1-8):

Please enter eight numbers: 1 2 3 4 5 6 7 8
The sum is: 36 

I am trying to redirect a file that has the eight numbers (e.g., "1 2 3 4 5 6 7 8" and a newline) to the program, but the input does not show on the screen:

./add8 < my_eight_numbers.txt
Please enter eight numbers: The sum is: 36

Is it possible to redirect the file into the program and show the input as if the user typed it?

  • Not that I know of. The screen is modeled as a character device that echos the input as it is received. In this case the device didn't receive the input -- it came from the file. You could modify the program to print the input that it received. – Andy Dalton Sep 11 '15 at 14:08

This might interest you.

Basically, you need to determine whether or not stdin in a terminal, or some sort of pipe/redirection. For that, you can use the isatty function.

int main(void){
    char buffer[N];
    int sum;

    /* Load buffer from stdin with fgets, fread, read, ... */

        printf("%s\n", buffer);

    /* Compute sum... */

    printf("The sum is %d.\n", sum);

In this case, when stdin is not a tty (which mean the input hasn't been typed in), the program prints the buffer before computing the sum.

On my system, the isatty function is declared in unistd.h. You will also need stdio.h to get fileno.


The c++ - way is to (sorry, i didn't program in c++ for quite a while)

  1. overload the ifstream '>>' - operator to also output all characters to stdout (ostream) on file read, and
  2. parse the positional parameters and pass them in as arrays then
  3. use that overloaded ifstream to read the file and calc the result

hope that helps


Normal terminals echo back what comes in. Seems like what you want to do is emulate this behavior if input is not a terminal and at the same time, output is.

You can test whether a filedescriptor is referring to a terminal with isatty(3).

In any case, doing this (interactive CLI programs, using isatty) is very much against the Unix spirit. Better not write programs like that. Cli programs should be filters if they can be.

  • Note that even the most basic programs such as ls use isatty in order to adapt their outputs. I don't think this is "against the UNIX way" at all, as long as you don't abuse it, for instance by making your program illogically work in two completely different ways depending on isatty's result. – John WH Smith Sep 14 '15 at 13:05
  • Well ls does it for formatting. It makes sense for formatting. But prompts like "please enter ... " aren't very unixy because they get in the way of reusing the output of one program as input of another. And removing the prompts depending on whether input is a terminal causes confusion. In short, interactivity gets in the way of scripting, and good Unix programs should be easily usable in scripts. But that's a philosophical point. isatty is the mechanism. – PSkocik Sep 14 '15 at 13:34
  • (I'm not trying to be antagonistic. I was the first to upvote your answer (sometimes I wish that were visible), and I wouldn't have written mine if I saw yours sooner than after I had submitted mine.) – PSkocik Sep 14 '15 at 13:39
  • isatty allows a program to act as filter and behave interactively at the same time. Another important point in UNIX philosophy is to not reinvent the wheel. If the interactive program can be (reasonably) tweaked so as to act a filter when appropriate, it probably should (note that I don't see too much of a conflict between our answers, they both bring valuable input). – John WH Smith Sep 14 '15 at 18:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.