Pat*_*ryk 5 c++ istream seek streambuf
我有以下基于此问题和答案的实现
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...
}
};
Run Code Online (Sandbox Code Playgroud)
我想通过以下方式在我的方法中使用它:
char buffer[] = { 0x01, 0x0a };
membuf sbuf(buffer, buffer + sizeof(buffer));
std::istream in(&sbuf);
Run Code Online (Sandbox Code Playgroud)
然后调用,例如tellg(),in并获得正确的结果。
到目前为止,它几乎是完美的-它不会在流的尽头停止。
我应该如何升级才能使其正常工作?
我的主要动机是模仿std::ifstream行为,但char[]在测试中将二进制文件输入到行为中(而不是依赖二进制文件)。
接受的答案不适用于将搜索方向设置为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();
}
Run Code Online (Sandbox Code Playgroud)
看来我错过了当前位置的回报。所以最终的实现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();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1180 次 |
| 最近记录: |