Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > An easy stringstream question

Reply
Thread Tools

An easy stringstream question

 
 
utab
Guest
Posts: n/a
 
      02-06-2007
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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      02-06-2007
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


 
Reply With Quote
 
 
 
 
utab
Guest
Posts: n/a
 
      02-06-2007

> 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

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      02-06-2007
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


 
Reply With Quote
 
Marcus Kwok
Guest
Posts: n/a
 
      02-06-2007
utab <(E-Mail Removed)> 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
 
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
Simple stringstream question WP C++ 4 10-11-2008 04:20 AM
stringstream question DaLoverhino C++ 2 09-19-2007 05:25 PM
stringstream question Grey Alien C++ 5 07-19-2007 08:31 AM
stringstream question (repost) Grey Alien C++ 1 07-17-2007 05:20 PM
stringstream question Marc Schellens C++ 2 11-04-2003 06:18 AM



Advertisments