May I ask iostream if it is available for recording?

In my code, I passed a link to iostream, which in this case is ultimately a file.
Is there a way to find out if it was established when it was opened, [in | out] not only [in]?

+7
source share
3 answers

After viewing all the members of IOS, iostream, ostream, I did not see a way to extract the openmode flag. I think you will have to try to write something on your link and catch any mistake that it throws. Or depending on how it is installed, check the result of bad ().

+3
source

openmode is passed before std::streambuf , but there are no methods to extract it. The only way to find out if a file is open for write operations is to write and check for f.fail() or optionally f.bad() (equivalent to f.rdstate() & std::fstream::badbit ).

+1
source

... view

 if ( dynamic_cast<ostream*>( MyStreamPtr ) == 0 ) { // Not for output... } 
-2
source

All Articles