I have a very simple stream buffer setup that forwards the bytes from a stream to the buffer. When I print I get a weird output, namely "HellHelllo,"
instead of "Hello"
. Maybe my eyes are tired, but I can't find the problem here. Why am I getting is output?
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <sstream>
class io_buffer : public std::streambuf
{
public:
io_buffer(std::istream& is, int buf_size = 4)
: size(buf_size) , is_(is) , buffer(std::max(buf_size, 1))
{
char* end = &buffer.front() + buffer.size();
setg(end, end, end);
}
int_type underflow()
{
if (gptr() < egptr())
return traits_type::to_int_type(*gptr());
if (is_)
{
std::copy_n(std::istreambuf_iterator<char>(is_),
size, std::back_inserter(buffer));
char* beg = &buffer.front();
setg(beg, beg, beg + buffer.size());
return traits_type::to_int_type(*gptr());
}
return traits_type::eof();
}
private:
int size;
std::istream& is_;
std::vector<char> buffer;
};
int main()
{
std::istringstream oss("Hello, World");
io_buffer buf(oss);
std::istream is(&buf);
std::string str;
is >> str;
std::cout << str << std::endl; // "HellHelllo,"
}