c++ - 如何通过自定义的 streambuf 设置流的 badbit

标签 c++ stream std

我有一个自定义的 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() 函数应该抛出异常吗?

最佳答案

抛出异常确实是可行的方法。

引用页面std::ios_base::iostate ,特别是这个:

The badbit is set by the following standard library functions:

  • ...
  • Every stream I/O function if an exception is thrown by any member function of the associated stream buffer (e.g. sbumpc(), xsputn(), sgetc(), overflow(), etc)
  • ...

关于c++ - 如何通过自定义的 streambuf 设置流的 badbit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50716688/

相关文章:

JavaScript 和 Google Chrome,输入流和数组

c++ - 访问 map<int,string[4]> 中的元素

c++ - C++ 中 std::cout 的奇怪行为

c++ - 为 std::vector 分配几 GB 内存

c# - 在 C++ 中将 std::string 用于非托管 C# dll

c++ - 使用 Visual Studio 2012 构建 Eclipse C++ Docfrac 项目

c++ - 在不更改流位置的情况下获取流大小

Java 字符串与流

c++ - Boost property_tree 用于存储指针

C++构造函数继承错误