Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   extracting data from string streams (http://www.velocityreviews.com/forums/t447616-extracting-data-from-string-streams.html)

Dylan 08-10-2005 08:44 AM

extracting data from string streams
 
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!

Maxim Yegorushkin 08-10-2005 08:58 AM

Re: extracting data from string streams
 

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.


Dylan 08-10-2005 09:11 AM

Re: extracting data from string streams
 
On 10 Aug 2005 01:58:29 -0700, "Maxim Yegorushkin"
<maxim.yegorushkin@gmail.com> 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?

Maxim Yegorushkin 08-10-2005 02:10 PM

Re: extracting data from string streams
 

Dylan wrote:
> On 10 Aug 2005 01:58:29 -0700, "Maxim Yegorushkin"
> <maxim.yegorushkin@gmail.com> 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



All times are GMT. The time now is 03:51 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.