471,585 Members | 1,227 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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 4767
"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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Woodster | last post: by
6 posts views Thread by Nemok | last post: by
4 posts views Thread by Vijai Kalyan | last post: by
5 posts views Thread by Marcin Kalicinski | last post: by
7 posts views Thread by Grey Alien | last post: by
5 posts views Thread by rtrujillor | last post: by
3 posts views Thread by Rune Allnor | last post: by
reply views Thread by XIAOLAOHU | last post: by

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.