I have the following implementation based on, for example, this question and answer
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);
}
}
};
I would like to use it in my methods as follows:
char buffer[] = { 0x01, 0x0a };
membuf sbuf(buffer, buffer + sizeof(buffer));
std::istream in(&sbuf);
and then call for example. tellg()on inand get the correct result.
While this is almost perfect - it does not stop at the end of the stream.
How do I update it so that it works correctly?
My main motivation is to simulate behavior std::ifstream, but with the binary char[]filed in them in the tests (instead of relying on binary files).
source
share