string stream question

Discussion in 'C++' started by JM, Sep 13, 2004.

  1. JM

    JM Guest

    Hi,


    If you change the same string stream to a new string, why is it that
    you have to .clear() it before using it? If I don't I get garbage.

    Thanks,
    Jason

    ex.

    string a = "12";
    string b = "15";
    float fa,fb;

    istringstream is;

    is.str(a);

    is>>fa;

    is.clear( ) // must do this....

    is.str(b)

    is>>fb
    JM, Sep 13, 2004
    #1
    1. Advertising

  2. JM wrote:
    > Hi,
    >
    >
    > If you change the same string stream to a new string, why is it that
    > you have to .clear() it before using it? If I don't I get garbage.
    >
    > Thanks,
    > Jason
    >
    > ex.
    >
    > string a = "12";
    > string b = "15";
    > float fa,fb;
    >
    > istringstream is;
    >
    > is.str(a);
    >
    > is>>fa;
    >
    > is.clear( ) // must do this....
    >
    > is.str(b)
    >
    > is>>fb



    It probably because changing strings is not coupled with clearing
    any errors. The state of the stream is still "end-of-file", which
    you have to clear before you can read again.

    Victor
    Victor Bazarov, Sep 13, 2004
    #2
    1. Advertising

  3. JM

    JM Guest

    On Mon, 13 Sep 2004 16:45:33 -0400, Victor Bazarov
    <> wrote:


    >
    >It probably because changing strings is not coupled with clearing
    >any errors. The state of the stream is still "end-of-file", which
    >you have to clear before you can read again.
    >
    >Victor


    That makes sense, thanks Victor.

    -JM
    JM, Sep 13, 2004
    #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. Rasmusson, Lars
    Replies:
    1
    Views:
    710
    popov
    Apr 30, 2004
  2. Replies:
    9
    Views:
    610
    Alex Buell
    Apr 27, 2006
  3. Alexander Korsunsky

    get stream mode flags from an opened stream

    Alexander Korsunsky, Feb 17, 2007, in forum: C++
    Replies:
    1
    Views:
    440
    John Harrison
    Feb 17, 2007
  4. dolphin
    Replies:
    6
    Views:
    542
    Thomas Fritsch
    Mar 18, 2007
  5. mrstephengross
    Replies:
    3
    Views:
    389
    James Kanze
    May 10, 2007
Loading...

Share This Page