473,748 Members | 8,760 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

extracting data from string streams

I'm attempting to write an object's state to a std::stringstre am 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::stringstre am 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 3848

Dylan wrote:
I'm attempting to write an object's state to a std::stringstre am 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::stringstre am 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::stringstre am 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::stringstre am 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::stringstre am 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::stringstre am 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::s tr() 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
3000
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, GAMESS=/opt/gamess,GAUSS_ARCHDIR=/opt/g03b05/g03/arch, GAUSS_EXEDIR=/opt/g03b05/g03/bsd:/opt/g03b05/g03/private:/opt/g03b05/g
2
1874
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 second one is used in the first one such as a field. I wrote the scrivi() function to write the record in a file called "archivio.txt" and it works fine. The I wrote the carica() function to load the records that I have placed on the file, but I...
2
2814
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 C#. The only significant problem that I have encountered in the conversion is this one - extracting an icon from the 'KTEntryPoint' program into the software suite and placing that icon on the PC Desktop.
13
3732
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 these methods EXTRACT the data at one point or another. The other problem is there appears to be NO WAY to get at the actual buffer pointer (char*) of the characters in the stream. There is a way to get the streambuf object associated with the...
1
1917
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 public string XslTransform(string style, string strdata) { MSXML2.IXMLDOMDocument pDoc = new MSXML2.DOMDocumentClass(); pDoc.async = false; pDoc.loadXML(strdata);
18
1668
by: v4vijayakumar | last post by:
is there any standard way to extract output from system(3) function? TIA.
0
1376
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 get preg_match() to work.) The substr() pulled it out, but read past the length I entered by 12 characters to include "endstream en". Besides that minor problem, I tried gzuncompress() on the extracted string which only generated a data error....
3
1605
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). How can I receive that string from jsp to asp? But, these aspx pages will be called by jsp only.
4
6229
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. I'm using AES for it is quick, and RSA to encrypt AES, to transmit AES keys (it'll run on two separate computers). Please notice that - this is my first C# code :)
0
8991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9374
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9325
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8244
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6076
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4876
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2787
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.