istream :: tellg()返回-1与我的自定义streambuf类一起使用时? [英] istream::tellg() returns -1 when used with my custom streambuf class?
问题描述
我尝试创建直接从原始内存缓冲区读取的 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依赖于受保护的虚函数 I'm trying to create an I found a nice way to do this in another post on here: Then I create my I then read using 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 The defaults in 这篇关于istream :: tellg()返回-1与我的自定义streambuf类一起使用时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! class p>
basic_streambuf
中的默认值只返回 pos_type(off_type(-1))
用于所有调用(对于许多实现可能等于-1)。istream
that reads directly from a raw memory buffer. class membuf : public basic_streambuf<char>
{
public:
membuf(char* p, size_t n) {
setg(p, p, p + n);
}
};
istream
using this membuf
: membuf mb(dataPointer, dataLength);
istream reader(&mb);
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
.seekoff
and seekpos
, that you would have to implement in your membuf
class.basic_streambuf
just returns pos_type(off_type(-1))
for all calls (which might be equal to -1 for many implementaions).