how to check whether istringstream object has empty buffer?

Discussion in 'C++' started by mshngo, May 7, 2005.

  1. mshngo

    mshngo Guest

    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
     
    mshngo, May 7, 2005
    #1
    1. Advertising

  2. 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
     
    Jonne Lehtinen, May 7, 2005
    #2
    1. Advertising

  3. mshngo

    Kanenas Guest

    On 7 May 2005 14:50:11 -0700, "mshngo" <> 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
     
    Kanenas, May 18, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Curt_C [MVP]
    Replies:
    1
    Views:
    534
  2. Shivanand Kadwadkar
    Replies:
    83
    Views:
    4,892
    Keith Thompson
    Jan 8, 2011
  3. Gene
    Replies:
    0
    Views:
    453
  4. mark4asp
    Replies:
    5
    Views:
    182
    Zenobia
    Jul 7, 2004
  5. king
    Replies:
    2
    Views:
    153
Loading...

Share This Page