Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check in C++ whether std::cout is redirected to a file?

I need my program to behave differently, depending on whether the output is to a terminal or to a file. How can I find this out from within C++?

I assume there is no solution that works for all operating systems. For my purposes, it would be good to have one strategy which works under Windows and one which works under linux/unix.

Thanks in advance.

like image 864
Egon Avatar asked Sep 25 '11 21:09

Egon


1 Answers

This will help under linux: How to tell if running in a linux console versus an ssh session?

Yes it is a C call, but it can definitely be called from C++.

GetStdHandle gives a similar starting point under windows:

like image 76
Danny Staple Avatar answered Oct 22 '22 00:10

Danny Staple