[SOLVED] Detect if stdin is a terminal or pipe?


When I execute “python” from the terminal with no arguments it brings up the Python interactive shell.

When I execute “cat | python” from the terminal it doesn’t launch the interactive mode. Somehow, without getting any input, it has detected that it is connected to a pipe.

How would I do a similar detection in C or C++ or Qt?


Use isatty:

#include <stdio.h>
#include <io.h>
if (isatty(fileno(stdin)))
    printf( "stdin is a terminal\n" );
    printf( "stdin is a file or a pipe\n");

(On windows they’re prefixed with underscores: _isatty, _fileno)

Answered By – RichieHindle

Answer Checked By – Cary Denson (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *