473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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!
Aug 10 '05 #1
3 3830

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.

Aug 10 '05 #2
On 10 Aug 2005 01:58:29 -0700, "Maxim Yegorushkin"
<ma***************@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?
Aug 10 '05 #3

Dylan wrote:
On 10 Aug 2005 01:58:29 -0700, "Maxim Yegorushkin"
<ma***************@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

Aug 10 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Steve | last post by:
Hi, I have a very long string, someting like: DISPLAY=localhost:0.0,FORT_BUFFERED=true, F_ERROPT1=271\,271\,2\,1\,2\,2\,2\,2,G03BASIS=/opt/g03b05/g03/basis,...
2
by: Aleander | last post by:
Hi! I have to write the records of a vector in a file, e and then open this file to extract the record to refill the vector. My program has two class: Visita(Appointment) and Data(date). The...
2
by: Dickyb | last post by:
Extracting an Icon and Placing It On The Desktop (C# Language) I constructed a suite of programs in C++ several years ago that handle my financial portfolio, and now I have converted them to...
13
by: Randy | last post by:
Is there any way to do this? I've tried tellg() followed by seekg(), inserting the stream buffer to an ostringstream (ala os << is.rdbuf()), read(), and having no luck. The problem is, all of...
1
by: Daniel | last post by:
Does system.xml have any way to transofrm data with an xswl style sheet using strings like MSXML2 does? how to convert this to use System.XML so i do not depend on MSXML2 interop? static...
18
by: v4vijayakumar | last post by:
is there any standard way to extract output from system(3) function? TIA.
0
by: runner7 | last post by:
I used file_get_contents() to read a pdf into a string and then tried to extract the encoded part between the "stream" and "endstream" words using the strpos() and substr() functions. (I could not...
3
by: =?Utf-8?B?Sm9obg==?= | last post by:
Hi all We are developing an application which has jsp and aspx pages. Here, I need to get large string as an input from jsp to aspx.(for ex: the length of this string is 10000 characters). ...
4
by: floppyzedolfin | last post by:
Hello! I'm actually encoding an encryption / decryption program. The encryption programes takes a file path in parameter, and encrypts the contents of the file and stores that into another file. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.