2

I have a test where I get characters out of an std::istringstream. I would like to get the section of the std::istringstream that was not read during the test. The std::istringstream::str() function returns the entire string, not just the unread section.

How do I get just this section of the string?

sgarizvi
  • 16,623
  • 9
  • 64
  • 98
Graznarak
  • 3,626
  • 4
  • 28
  • 47

2 Answers2

4

Assuming you don't manually set the input position indicator before this line:

std::string unread = stream.eof()  ?  "" : stream.str().substr( stream.tellg() );
David G
  • 94,763
  • 41
  • 167
  • 253
0

The simplest way to do this I think is to push the read buffer of the istringstream to a string stream:

void test(std::istringstream& iss)
{
  /* Test code */
}

int main()
{
   std::istringstream iss;
   std::stringstream not_readed_buffer;
  std::string not_readed;

   test( iss );

   if( iss )
   {
       not_readed_buffer << iss.rdbuf();
       not_readed = not_readed_buffer.str();    
   }
}

Check this answer for a similar question

Community
  • 1
  • 1
Manu343726
  • 13,969
  • 4
  • 40
  • 75