简体   繁体   English

我可以问一个 iostream 是否可写吗?

[英]Can I ask an iostream if it is writeable?

In my code I am passed a reference to an iostream, that in this case is ultimately a file.在我的代码中,我传递了对 iostream 的引用,在这种情况下,它最终是一个文件。
Is there any way to find out if, when it was opened, it was set to [in|out] not just [in]?有什么方法可以查明,当它被打开时,它是否被设置为 [in|out] 而不仅仅是 [in]?

After looking at all the members of IOS, iostream, ostream, i did not see any way to retrieve the openmode flag used.在查看了 IOS、iostream、ostream 的所有成员之后,我没有看到任何方法可以检索使用的openmode标志。 I guess you'll have to try to write something to your reference and catch any error it throws.我想您将不得不尝试为您的参考写一些东西并捕获它抛出的任何错误。 Or depending on how it is set, check the result of bad().或者根据它的设置方式,检查 bad() 的结果。

The openmode is passed down to std::streambuf , but there are no methods to retrieve it. openmode被传递给std::streambuf ,但没有方法可以检索它。 The only way to know if the file is open for write operations is trying to write and checking for f.fail() , or optionally f.bad() (equivalent to f.rdstate() & std::fstream::badbit ).知道文件是否为写入操作打开的唯一方法是尝试写入并检查f.fail()或可选f.bad() (相当于f.rdstate() & std::fstream::badbit ) .

...kind of ...有点儿

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM