【问题标题】:how to set the badbit of a stream by a customized streambuf如何通过自定义的streambuf设置流的badbit
【发布时间】:2018-06-06 09:21:09
【问题描述】:

我有一个自定义的std::streambuf。比如:

class mybuf : public std::streambuf {
  ...
  virtual std::streambuf::int_type underflow() {
    if(eof) {
     return traits_type::eof();
    }
    ...
  }
  ...
  std::istream stream(new mybuf(...));

通过在 underflow() 函数上返回 eof 来设置流的 eof 标志。

如何设置流的badbit 标志? overflow() 函数是否应该抛出异常?

【问题讨论】:

标签: c++ stream std


【解决方案1】:

抛出异常确实是要走的路。

请参阅std::ios_base::iostate 的参考页,特别是这个:

badbit 由以下标准库函数设置:

  • ...
  • 如果关联流缓冲区的任何成员函数(例如sbumpc()xsputn()sgetc()overflow() 等)抛出异常,则每个流 I/O 函数
  • ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-18
    • 1970-01-01
    • 2016-05-06
    • 2013-05-02
    • 1970-01-01
    • 1970-01-01
    • 2011-03-30
    • 1970-01-01
    相关资源
    最近更新 更多