浙江省高等学校教师教育理论培训

微信搜索“教师资格证岗前培训”小程序

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

c++ - safe reading from a stream in a for loop using getline - Stack Overflow

The iostreams by default do not throw exceptions when errors occur. If you want to enable them:

cout.execeptions( std::ios::badbit );

would enable exceptions if badbit is set.

To enable them all:

cout.execeptions( std::ios::badbit 
                   | std::ios::eofbit 
                   | std::ios::failbit );

The exceptions thrown are of type:

std::ios_base::failure

which is derived from std::exception.

In general though, it is easier not to use execptions, but to use constructs like:

while( std::getline( myStreamObj, line ) ) {
   // process line
}
posted on 2012-10-25 13:31  lexus  阅读(207)  评论(0编辑  收藏  举报