Check if ostream object is cout or ofstream, c++

后端 未结 4 1877
北恋
北恋 2020-12-06 11:26

Is there a way in C++ to check if an ostream object is cout or a ofstream object?

Something like:

ostream& ou         


        
4条回答
  •  伪装坚强ぢ
    2020-12-06 12:06

    It sounds like what you really want to know is not whether the stream is cout but whether the underlying file descriptor is attached to a terminal? If that is so, you need the underlying file descriptor. Unfortunately, you can't get that from a iostream. If it's possible to use cstdio instead of iostream, then you can. If you do have the file descriptor, determining if you are writing to a terminal is a simple as seeing if tcgetattr() returns -1.

    Also, don't let anyone tell you not do implement some functionality that you need because it tarnishes the purity of some leaky abstraction. If you really need different behavior, then do what you need to do to produce that functionality.

提交回复
热议问题