在C ++中,你可以手动设置流的failbit吗?怎么样? [英] In C++, can you manually set the failbit of a stream? How?
本文介绍了在C ++中,你可以手动设置流的failbit吗?怎么样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我重载输入流运算符以与Time类一起使用,并且如果输入不匹配我的预期时间格式(hh:mm),则希望手动设置输入流的failbit。这可以做吗?如何?
I am overloading the input stream operator for use with a Time class and would like to manually set the failbit of the input stream if the input doesn't match my expected time format (hh:mm). Can this be done? How?
谢谢!
推荐答案
ios :: setstate
,如下所示:
#include <iostream>
#include <ios>
int main()
{
std::cout << "Hi\n";
std::cout.setstate(std::ios::failbit);
std::cout << "Fail!\n";
}
第二个输出不会产生,因为 cout
处于失败状态。
The second output will not be produced because cout
is in the failed state.
(一个例外似乎更清洁我,但YMMV)
(An exception seems cleaner to me, but YMMV)
这篇关于在C ++中,你可以手动设置流的failbit吗?怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文