There are no portable devices. On Unix, you can:
if ( (&os == &std::cout && isatty( STDOUT )) || (&os == &std::cerr && isatty( STDERR )) || (&os == &std::clog && isatty( STDERR )) ) } // is a terminal... }
On Windows, isatty becomes _isatty , and I'm not sure if macros exist (but I suspect that they do).
Of course, this assumes that you are not doing something to confuse it in your code. Something like:
std::ostream s( std::cout.rdbuf() );
for example: or
std::cout.rdbuf( &someFileBuf );
Or even:
std::ofstream s( "/dev/tty" ); // (or "CONS" under Windows).
But this is about as close as you can get without actual fd from filebuf .
James kanze
source share