Domanda

I have a istringstream called 'is' and contains the string "101a0101". My code here:

cout << is.str() << endl;

char bit;
while (is >> bit) {
    if (bit == '0') /* do stuff */;
    else if (bit == '1') /* do stuff */;
    else break;
}

cout << is.str() << endl;

Here's my output

output:   
101a0101
101a0101

Why isn't my istringstream consuming characters?

È stato utile?

Soluzione

Accessing the content of the stream using str() will return you then entire character sequence in the buffer. If you need to access the unread subsequence of the stream, you can use substr() with tellg():

std::string unread = is.str().substr(is.tellg());
std::cout << unread; // "a0101"
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top