Here i have some of researching over streambuffer, read directly to string from istream by constructor:
class mystringbuf : public std::stringbuf
{
public:
explicit mystringbuf(std::istream& istr, size_t n,
std::ios_base::openmode __mode = std::ios_base::in )
{
_M_string.resize(n);
std::stringbuf::_M_stringbuf_init(__mode);
istr.read(gptr(), n);
}
public:
std::stringbuf::char_type* gptr() const
{
return std::stringbuf::gptr();
}
std::string& str_ref(){
return _M_string;
}
};
std::ostream& operator << (std::ostream& ostr, mystringbuf& buf){
ostr << buf.str_ref();
return ostr;
}
Example to use:
using std::cout;
using std::endl;
int main()
{
std::stringbuf buffer; // empty buffer
buffer.str("abc def ABC DEF "); // not empty now
std::istream is (&buffer); // associate stream buffer to stream
mystringbuf data(is, 10); // read 10 bytes
cout << "data=" << data << endl;
return 0;
}
Output:
data=abc def AB
Please forward me if i`am somewhere was wrong.