Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > extracting data from string streams

Reply
Thread Tools

extracting data from string streams

 
 
Dylan
Guest
Posts: n/a
 
      08-10-2005
I'm attempting to write an object's state to a std::stringstream and
then to restore that state by reading from the stringstream. The
writing is fine but I'm having problems reading. Here's a little prog
that demonstrates the problem:

#include <iostream>
#include <sstream>

int main()
{
//create a buffer containing "1,2,3"
std::stringstream ss;
ss << 1 << "," << 2 << "," << 3;
std::cout << "\n" << ss.str();

//I'd like to assign 1 to iVal1 and 2 to iVal2, leaving ",3" in ss
//(but the following doesn't work)
int iVal1, iVal2;
ss >> iVal1 >> iVal2;
std::cout << "\n" << iVal1 << "," << iVal2;
std::cout << "\n" << ss.str();

return 0;
}

//The output from this is
1,2,3
1, -858993460
1,2,3

//but I'd like it to be
1,2,3
1,2
3,

How can i achieve this?

Thanks!
 
Reply With Quote
 
 
 
 
Maxim Yegorushkin
Guest
Posts: n/a
 
      08-10-2005

Dylan wrote:
> I'm attempting to write an object's state to a std::stringstream and
> then to restore that state by reading from the stringstream. The
> writing is fine but I'm having problems reading. Here's a little prog
> that demonstrates the problem:
>
> #include <iostream>
> #include <sstream>
>
> int main()
> {
> //create a buffer containing "1,2,3"
> std::stringstream ss;
> ss << 1 << "," << 2 << "," << 3;
> std::cout << "\n" << ss.str();
>
> //I'd like to assign 1 to iVal1 and 2 to iVal2, leaving ",3" in ss
> //(but the following doesn't work)
> int iVal1, iVal2;
> ss >> iVal1 >> iVal2;
> std::cout << "\n" << iVal1 << "," << iVal2;
> std::cout << "\n" << ss.str();
>
> return 0;
> }
>
> //The output from this is
> 1,2,3
> 1, -858993460
> 1,2,3
>
> //but I'd like it to be
> 1,2,3
> 1,2
> 3,
>
> How can i achieve this?


Standart streams use whitespaces as delimeters. To achive what you want
use spaces or tabs instead of commas when outputting.

 
Reply With Quote
 
 
 
 
Dylan
Guest
Posts: n/a
 
      08-10-2005
On 10 Aug 2005 01:58:29 -0700, "Maxim Yegorushkin"
<(E-Mail Removed)> wrote:

>
>Dylan wrote:
>> I'm attempting to write an object's state to a std::stringstream and
>> then to restore that state by reading from the stringstream. The
>> writing is fine but I'm having problems reading. Here's a little prog
>> that demonstrates the problem:
>>
>> #include <iostream>
>> #include <sstream>
>>
>> int main()
>> {
>> //create a buffer containing "1,2,3"
>> std::stringstream ss;
>> ss << 1 << "," << 2 << "," << 3;
>> std::cout << "\n" << ss.str();
>>
>> //I'd like to assign 1 to iVal1 and 2 to iVal2, leaving ",3" in ss
>> //(but the following doesn't work)
>> int iVal1, iVal2;
>> ss >> iVal1 >> iVal2;
>> std::cout << "\n" << iVal1 << "," << iVal2;
>> std::cout << "\n" << ss.str();
>>
>> return 0;
>> }
>>
>> //The output from this is
>> 1,2,3
>> 1, -858993460
>> 1,2,3
>>
>> //but I'd like it to be
>> 1,2,3
>> 1,2
>> 3,
>>
>> How can i achieve this?

>
>Standart streams use whitespaces as delimeters. To achive what you want
>use spaces or tabs instead of commas when outputting.


ok, thanks. That helps partly, but using the >> operator doesn't
remove the values from the stream. Do you know how I can remove
values?
 
Reply With Quote
 
Maxim Yegorushkin
Guest
Posts: n/a
 
      08-10-2005

Dylan wrote:
> On 10 Aug 2005 01:58:29 -0700, "Maxim Yegorushkin"
> <(E-Mail Removed)> wrote:
>
> >
> >Dylan wrote:
> >> I'm attempting to write an object's state to a std::stringstream and
> >> then to restore that state by reading from the stringstream. The
> >> writing is fine but I'm having problems reading. Here's a little prog
> >> that demonstrates the problem:
> >>
> >> #include <iostream>
> >> #include <sstream>
> >>
> >> int main()
> >> {
> >> //create a buffer containing "1,2,3"
> >> std::stringstream ss;
> >> ss << 1 << "," << 2 << "," << 3;
> >> std::cout << "\n" << ss.str();
> >>
> >> //I'd like to assign 1 to iVal1 and 2 to iVal2, leaving ",3" in ss
> >> //(but the following doesn't work)
> >> int iVal1, iVal2;
> >> ss >> iVal1 >> iVal2;
> >> std::cout << "\n" << iVal1 << "," << iVal2;
> >> std::cout << "\n" << ss.str();
> >>
> >> return 0;
> >> }
> >>
> >> //The output from this is
> >> 1,2,3
> >> 1, -858993460
> >> 1,2,3
> >>
> >> //but I'd like it to be
> >> 1,2,3
> >> 1,2
> >> 3,
> >>
> >> How can i achieve this?

> >
> >Standart streams use whitespaces as delimeters. To achive what you want
> >use spaces or tabs instead of commas when outputting.

>
> ok, thanks. That helps partly, but using the >> operator doesn't
> remove the values from the stream. Do you know how I can remove
> values?


operator>> does not change the buffer, but it does move the buffer read
pointer.
stringstream::str() returns a copy of the whole buffer, no matter where
the read pointer points to.

To clear a stringstream do:

ss.str(""); // clear the buffer
ss.clear(); // reset stream flags

 
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
extracting data from std::set<string> Rene Ivon Shamberger C++ 2 10-19-2012 03:21 PM
Reading unsigned values from string streams. BigMan C++ 3 06-12-2005 06:05 PM
String to Long Double using Streams ferran C++ 5 09-03-2004 11:49 AM
Extracting a data from a string Dan Perl Misc 4 05-28-2004 09:55 PM
Number conversion with string streams Eugene C++ 0 07-30-2003 10:58 PM



Advertisments