7

我有以下基于例如这个问题和答案的实现

struct membuf : std::streambuf
{
  membuf(char* begin, char* end)
  {
    this->setg(begin, begin, end);
  }

protected:
  virtual pos_type seekoff(off_type off,
                           std::ios_base::seekdir dir,
                           std::ios_base::openmode which = std::ios_base::in)
  {
    std::istream::pos_type ret;
    if(dir == std::ios_base::cur)
    {
      this->gbump(off);
    }
    // something is missing here...
  }
};

我想通过以下方式在我的方法中使用它:

  char buffer[] = { 0x01, 0x0a };
  membuf sbuf(buffer, buffer + sizeof(buffer));
  std::istream in(&sbuf);

然后调用 eg tellg()onin并获得正确的结果。

到目前为止,它几乎是完美的——它不会在流的尽头停止。

我应该如何升级它以使其正常工作?

我的主要动机是模仿std::ifstream行为,但char[]在测试中将二进制文件输入它们(而不是依赖二进制文件)。

4

2 回答 2

9

接受的答案不适用于搜索方向设置为std::ios_base::beg或的情况std::ios_base::end。为了支持这些情况,通过以下方式扩展实现:

pos_type seekoff(off_type off,
                 std::ios_base::seekdir dir,
                 std::ios_base::openmode which = std::ios_base::in) {
  if (dir == std::ios_base::cur)
    gbump(off);
  else if (dir == std::ios_base::end)
    setg(eback(), egptr() + off, egptr());
  else if (dir == std::ios_base::beg)
    setg(eback(), eback() + off, egptr());
  return gptr() - eback();
}
于 2017-09-06T07:08:54.117 回答
5

看来我错过了当前职位的回报。所以最终的实现seekoff看起来像:

  pos_type seekoff(off_type off,
                   std::ios_base::seekdir dir,
                   std::ios_base::openmode which = std::ios_base::in)
  {
    if (dir == std::ios_base::cur) gbump(off);

    return gptr() - eback();
  }
于 2016-01-28T16:30:41.117 回答