Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > how to check whether istringstream object has empty buffer?

Reply
Thread Tools

how to check whether istringstream object has empty buffer?

 
 
mshngo
Guest
Posts: n/a
 
      05-07-2005
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

 
Reply With Quote
 
 
 
 
Jonne Lehtinen
Guest
Posts: n/a
 
      05-07-2005
mshngo wrote:
> 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
 
Reply With Quote
 
 
 
 
Kanenas
Guest
Posts: n/a
 
      05-18-2005
On 7 May 2005 14:50:11 -0700, "mshngo" <(E-Mail Removed)> wrote:

>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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to check whether malloc has allocated memory properly in case ifmalloc(0) can return valid pointer Shivanand Kadwadkar C Programming 83 01-08-2011 08:18 AM
How to check whether user put a check in a toolbox CheckBox contro JB ASP .Net 1 08-26-2009 11:18 PM
how to check whether the field is filled or empty in perl TK king Perl Misc 2 11-15-2007 03:17 PM
Re: how can I code to check whether computer has installed correct version of .net framework? Curt_C [MVP] ASP .Net 1 02-17-2004 03:24 PM



Advertisments