An easy stringstream question

Discussion in 'C++' started by utab, Feb 6, 2007.

  1. utab

    utab Guest

    Dear all

    the code below, there is sth wrong with string streams. I can not
    bound the same string to a string stream second time. If I create
    another string stream it is OK. Most probably there is sth that I am
    missing about stream buffers.

    1 #include <iostream>
    2 #include <sstream>
    3 #include <string>
    4
    5 using namespace std;
    6
    7 int main(){
    8 unsigned index;
    9 unsigned i;
    10 char c;
    11 double val;
    12 string in("1 c 0.43");
    13 istringstream strm;
    14 strm.str(in);
    15 strm >> index >> c >> val;
    16 cout << index << " " << c << " " << val*0.2 << endl;
    17 //istringstream strm1(in.c_str());
    18 strm.str(in);
    19 strm >> i;
    20 cout << val << " " << i << endl;
    21 return 0;
    22 }

    Output is:
    1 c 0.086
    0.43 3086289584

    Best regards
     
    utab, Feb 6, 2007
    #1
    1. Advertising

  2. utab wrote:
    > the code below, there is sth wrong with string streams. I can not
    > bound the same string to a string stream second time. If I create
    > another string stream it is OK. Most probably there is sth that I am
    > missing about stream buffers.
    >
    > 1 #include <iostream>
    > 2 #include <sstream>
    > 3 #include <string>
    > 4
    > 5 using namespace std;
    > 6
    > 7 int main(){
    > 8 unsigned index;
    > 9 unsigned i;
    > 10 char c;
    > 11 double val;
    > 12 string in("1 c 0.43");
    > 13 istringstream strm;
    > 14 strm.str(in);
    > 15 strm >> index >> c >> val;
    > 16 cout << index << " " << c << " " << val*0.2 << endl;
    > 17 //istringstream strm1(in.c_str());


    At this point 'strm' is most likely not in a good state. Consider
    calling 'clear' for it.

    > 18 strm.str(in);
    > 19 strm >> i;
    > 20 cout << val << " " << i << endl;
    > 21 return 0;
    > 22 }
    >
    > Output is:
    > 1 c 0.086
    > 0.43 3086289584
    >
    > Best regards


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Feb 6, 2007
    #2
    1. Advertising

  3. utab

    utab Guest


    > At this point 'strm' is most likely not in a good state. Consider
    > calling 'clear' for it.


    I have also found that but what is the reason that it is not in good
    state.

    Thanks
     
    utab, Feb 6, 2007
    #3
  4. utab wrote:
    >> At this point 'strm' is most likely not in a good state. Consider
    >> calling 'clear' for it.

    >
    > I have also found that but what is the reason that it is not in good
    > state.


    The last read (into a number) ends by trying to read past the end of
    the string. That puts it into "end-of-file" state. You can try adding
    a space (or other non-digit character) into your sting past the last
    number and your stream will not acquire the 'eof' state while reading
    the number.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Feb 6, 2007
    #4
  5. utab

    Marcus Kwok Guest

    utab <> wrote:
    > the code below, there is sth wrong with string streams. I can not
    > bound the same string to a string stream second time. If I create
    > another string stream it is OK. Most probably there is sth that I am
    > missing about stream buffers.
    >
    > 1 #include <iostream>
    > 2 #include <sstream>
    > 3 #include <string>
    > 4
    > 5 using namespace std;
    > 6
    > 7 int main(){
    > 8 unsigned index;
    > 9 unsigned i;
    > 10 char c;
    > 11 double val;
    > 12 string in("1 c 0.43");
    > 13 istringstream strm;
    > 14 strm.str(in);
    > 15 strm >> index >> c >> val;
    > 16 cout << index << " " << c << " " << val*0.2 << endl;
    > 17 //istringstream strm1(in.c_str());


    Try adding:

    strm.clear();

    here.

    > 18 strm.str(in);
    > 19 strm >> i;
    > 20 cout << val << " " << i << endl;
    > 21 return 0;
    > 22 }
    >
    > Output is:
    > 1 c 0.086
    > 0.43 3086289584


    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
     
    Marcus Kwok, Feb 6, 2007
    #5
    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. Hazzard
    Replies:
    2
    Views:
    678
    Hazzard
    Apr 6, 2004
  2. Marc Schellens

    stringstream question

    Marc Schellens, Nov 4, 2003, in forum: C++
    Replies:
    2
    Views:
    432
    Marc Schellens
    Nov 4, 2003
  3. Grey Alien

    stringstream question

    Grey Alien, Jul 17, 2007, in forum: C++
    Replies:
    5
    Views:
    567
    James Kanze
    Jul 19, 2007
  4. Grey Alien

    stringstream question (repost)

    Grey Alien, Jul 17, 2007, in forum: C++
    Replies:
    1
    Views:
    287
    Victor Bazarov
    Jul 17, 2007
  5. Bruno Desthuilliers
    Replies:
    5
    Views:
    414
    Bruno Desthuilliers
    Aug 29, 2007
Loading...

Share This Page