istream :: tellg()返回-1与我的自定义streambuf类一起使用时? [英] istream::tellg() returns -1 when used with my custom streambuf class?

查看:1149
本文介绍了istream :: tellg()返回-1与我的自定义streambuf类一起使用时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试创建直接从原始内存缓冲区读取的 istream



在这里的另一个帖子中执行此操作的好方法:

  class membuf:public basic_streambuf< char> 
{
public:
membuf(char * p,size_t n){
setg(p,p,p + n);
}
};

然后我创建我的 istream code> membuf :

  membuf mb(dataPointer,dataLength); 
istream reader(& mb);

然后我使用 getline() >> 运算符,一切都很好。但是,我似乎不能使用 seekg()回到缓冲区的开头,而 istream :: tellg()总是返回 -1



我需要写更多的代码才能得到这些

$ p

解决方案

函数tellg和seekg依赖于受保护的虚函数 class p>

basic_streambuf 中的默认值只返回 pos_type(off_type(-1))用于所有调用(对于许多实现可能等于-1)。


I'm trying to create an istream that reads directly from a raw memory buffer.

I found a nice way to do this in another post on here:

  class membuf : public basic_streambuf<char>
  {
  public:
      membuf(char* p, size_t n) {
          setg(p, p, p + n);
      }
  };

Then I create my istream using this membuf:

    membuf mb(dataPointer, dataLength);
    istream reader(&mb);

I then read using getline() and >> operators, and everything is wonderful. However, I can't seem to use seekg() to rewind back to the beginning of my buffer, and istream::tellg() always returns -1.

Do I need to write some more code to get these to work, or is this doomed to failure?

解决方案

The functions tellg and seekg depends on protected virtual functions seekoff and seekpos, that you would have to implement in your membuf class.

The defaults in basic_streambuf just returns pos_type(off_type(-1)) for all calls (which might be equal to -1 for many implementaions).

这篇关于istream :: tellg()返回-1与我的自定义streambuf类一起使用时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆