how to check whether istringstream object has empty buffer?

M

mshngo

Hi,

What would be the best way to check whether an istringstream object has
empty buffer? I wanted to do something like:

istringstream isStr(myStringContent.c_str());
while (isStr_BUFFER_IS_NOT_EMPTY) {
//do something
}

I wanted to know how to implement isStr_BUFFER_IS_NOT_EMPTY. I did some
research on the google but still couldn't find a direct answer. I very
much appreciate your help.

Mingsheng
 
J

Jonne Lehtinen

mshngo said:
Hi,

What would be the best way to check whether an istringstream object has
empty buffer? I wanted to do something like:

istringstream isStr(myStringContent.c_str());
while (isStr_BUFFER_IS_NOT_EMPTY) {
//do something
}

I wanted to know how to implement isStr_BUFFER_IS_NOT_EMPTY. I did some
research on the google but still couldn't find a direct answer. I very
much appreciate your help.

Mingsheng

reading with >> sets the stream status depending on wether the read was
successful or not. If you read it to a string it shouldn't (as far as I
know) fail unless the buffer is empty.

so for example this crappy code I made quickly to test it

#include <iostream>
#include <string>
#include <sstream>

int main() {

std::string blah = "blah blah";
std::istringstream isStr(blah.c_str());
while( isStr >> blah ) {

std::cout << "blah" << std::endl;
}

std::cout << "bleh" << std::endl;
return 0;

}

outputs

blah
blah
bleh

- Jonne Lehtinen
 
K

Kanenas

Hi,

What would be the best way to check whether an istringstream object has
empty buffer? I wanted to do something like:
Check out basic_ios::rdbuf (which returns a pointer to the stream's
buffer) and basic_streambuf::in_avail (which returns the number of
unread characters in a buffer). Usage looks like:
while (isStr.rdbuf()->in_avail()) {/* do stuff */}
istringstream isStr(myStringContent.c_str());
while (isStr_BUFFER_IS_NOT_EMPTY) {
//do something
}

I wanted to know how to implement isStr_BUFFER_IS_NOT_EMPTY. I did some
research on the google but still couldn't find a direct answer. I very
much appreciate your help.
I also recommend that you consider the approach Jonne Lehtinen
suggested. Depending on what you hope to accomplish, checking for
error after extraction may be more appropriate than checking for
available characters.

Kanenas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top