TITLE: A non-portable way to detect redirection of stdio PROBLEM: gieseke@Informatik.Uni-Marburg.de (GEORG GIESEKE) How can one detect wether `cout' is redirected or not. In the latter case some `paging' is reqired, because a long list should be displayed( by my application). The only solution I know, is to call `sync_with_stdio()' and to inspect the `FILE'-structure of `stdout'. But is there a elegant solution using only streamfunctions? RESPONSE: kanze@us-es.sel.de (James Kanze), 24 May 94 There is, in fact, no portable solution. The following works on my system (Sun sparc, with both Sun CC and g++): #include #include #include int main() { if ( isatty( ((filebuf*)(cout.rdbuf()))->fd() ) ) cout << "To tty" << endl ; else cout << "To file" << endl ; return 0 ; } I suspect that it will work on all Unix systems, but I know of no guarantee that cout in fact uses a filebuf, and not some special type of streambuf. (You really need dynamic_cast for this.)