How can my program tell if it's output has been redirected?
This does the trick:
const bool bInteractive = isatty(fileno(stdout));
printf("We ARE interactive (no redirection)\n");
printf("We are NOT interactive (being redirected)\n");
This works on Linux and Borland C++ 5.x (which is nice).
The Linux "less" command uses this. If you do:
Then less pages the file. And you can scroll backwards and forwards.
But if you do:
less myfile.txt > anotherfile.txt
Then less does not offer any paging.