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

1

10

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.

Asphyxiant answered 25/9, 2011 at 21:56 Comment(0)
E
3

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:

Emaciation answered 25/9, 2011 at 22:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.