How can I tell if standard input or output is redirected

Q

How can I tell if standard input or output is redirected (i.e. whether ``<'' or ``>'' was used on the invocation command line)?

✍: Guest

A
You can't tell directly, but you can usually look at a few other things to make whatever decision you need to. If you want your program to take input from stdin when not given any input files, you can do so if argv doesn't mention any input files or perhaps if you're given a placeholder like ``-'' instead of a filename. If you want to suppress prompts if input is not coming from an interactive terminal, on some systems (e.g. Unix, and usually MS-DOS) you can use isatty(0) or isatty(fileno(stdin)) to make the determination.

2015-10-01, 793👍, 0💬