开发者

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

开发者 https://www.devze.com 2023-04-08 20:41 出处:网络
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 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 w开发者_JAVA百科orks under linux/unix.

Thanks in advance.


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:

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号