473,498 Members | 1,842 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::stringstream && std::string

Hello!

I am using std::stringstream && std::string to parse a text file ->

std::ifstream in;
std::string s;
std::streamstring ss;
int n;

I grab each line like so -> std::getline(in,s);

I then do this to get the string into a stringstream -> ss << s;

I am using a std::stringstream because it makes extracting data easier
like so -> ss >> n;

Is this the proper way to use std::stringstream? How do I blank out
the std::stringstream object because each time I do a (ss << s;) the
stream keeps concatenating onto itself...how do I blank out the stream
to add a new string to it?

Thanks.

Jul 22 '05 #1
1 4872
"KidLogik" <Si********@nospam.com> wrote in message
news:pf********************************@4ax.com...
Hi,
I am using std::stringstream && std::string to parse a text file ->

std::ifstream in;
std::string s;
std::streamstring ss; You mean stringstream I guess (and istringstream would actually be
best, once you follow the approach below).
int n;

I grab each line like so -> std::getline(in,s);

I then do this to get the string into a stringstream -> ss << s;

What you really want to do is either:
- use the str(s) member function: ss.str(s); // resets buffer to s
- re-create a new istringstream instance for every line:
std::istringstream ss(s);
(the latter will eventually be less efficient on some platforms).
Regards,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- e-mail contact form
Jul 22 '05 #2

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

Similar topics

2
7467
by: Woodster | last post by:
I am using std::stringstream to format a string. How can I clear the stringstream variable I am using to "re use" the same variable? Eg: Using std::string std::string buffer; buffer =...
6
11474
by: Nemok | last post by:
Hi, I am new to STD so I have some questions about std::string because I want use it in one of my projects instead of CString. 1. Is memory set dinamicaly (like CString), can I define for...
4
12133
by: Vijai Kalyan | last post by:
I wrote the following function as a curiosity: template<typename SourceType, typename DestinationType> DestinationType NumericCast(const SourceType& value) { std::wstringstream strbuf; strbuf...
5
4167
by: Marcin Kalicinski | last post by:
Is there a vectorstream class that implements the functionality similar to std::stringstream but with std::vector, not std::string? cheers, Marcin
5
3570
by: ma740988 | last post by:
Consider: #include <iostream> #include <sstream> #include <string> int main ( ) { { double pi = 3.141592653589793238; std::stringstream a;
7
3588
by: Ziyan | last post by:
I am writing a C/C++ program that runs in background (Linux). Therefore, normally no output would be written into standard output. However, sometimes I want to have debug message collected and sent...
7
13610
by: Grey Alien | last post by:
Does *ANYONE* in here know how I may parse the various date/time 'elements' from a string?. The input string has the ff format: 'YYYY-MM-DD HH:MM:SS AM'
5
3969
by: rtrujillor | last post by:
int iAnno = 2005; int iMes = 7; int iDia = 23; std::stringstream ssAnno; ssAnno << iAnno; std::string anno = ssAnno.str(); std::stringstream ssMes; ssMes << iMes;
3
5884
by: Rune Allnor | last post by:
Hi all. I am trying to use std::stringstream to validate input from a file. The strategy is to read a line from the file into a std::string and feed this std::string to an object which breaks it...
0
7125
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,...
0
7004
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7167
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5464
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,...
1
4915
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...
0
4593
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...
0
3085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1423
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
292
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...

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.