   Deriving streambuf for use in istream
  I''m lost here... I want to read text from a specialized input source, but I want to use getline() to do it. So I came up with the following, after looking through some STL documentation:   #include <iostream>#include <streambuf>class SpecialStreamBuf : public std::streambuf{public:   virtual int_type underflow()   { // Get next char from input source, or realize EOF if(eof_happened) {    return traits_type::eof(); } else {    return traits_type::to_int_type(char_that_i_read); }   }};int main(){   SpecialStreamBuf test_buf;   std::istream test_stream(&test_buf);   std::string input_text;   while(true)   { getline(test_stream, input_text); if(test_stream.fail()) {    break; }   }   return 0;}My problem is that I can trace the first underflow() call, the character is read correctly and returned, but there''s never a second underflow() call to read again.The getline call returns, and the stream is in a fail state so the program exits.Obviously, I''m doing something wrong here. But I can''t find any examples anywhere, and the documentation suggests that I''m actually doing it right, as underflow() should return the next character.Help? I really don''t have much experience with streams. 
  As far as I can tell the problem is that you aren''t maintaining an input buffer. The std::streambuf protocol for input stream_bufs is characterized by three pointers as well as the underflow() function: eback(), gptr() and egptr(). A std::istream object handles input by first checking the gptr() for data (first to make sure it''s non-null and then to make sure it precedes egptr()). If gptr() doesn''t seem to have data, then it calls underflow(). Part of the underflow() specification is that it sets gptr() to point to the character just returned. In practice, this means that an underflow() implementation that doesn''t return *gptr(); is probably ill-formed.This code snippet, while far from award winning, shows the minimum amount of scaffolding necessary to use with istream::getline():class my_buf : public std::streambuf {  protected:    char buffer[1];    virtual int_type underflow() { setg(buffer, buffer, buffer + 1); if ((rand() % 100) == 0) return traits_type::eof(); *gptr() = (rand() % 2 == 0) ? ''a'' : ''\n''; return *gptr();    }};  Obviously replace the rand() % 100 call with your own EOF detection and the rand() % 2 stuff with your actual character getting routines.Keep in mind this is an inefficient way to use a stream_buf. stream_buf objects are supposed to do actual buffering, not just read characters one at a time.;  Ahh, I see. My problem was that the only example I found was for the overflow() method, which was writing one char at a time. I didn''t realize you needed to maintain a buffer for input.I got it now though, thanks a lot! I created a member variable buffer (of a size larger than 1 char), and when underflow is called I fill the buffer with as much data as I can, then call setg to setup the begin, next and end pointers. 