Connecting Tech Pros Worldwide Help | Site Map

std::stringstream && std::string

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 06:48 AM
KidLogik
Guest
 
Posts: n/a
Default 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.


  #2  
Old July 22nd, 2005, 06:48 AM
Ivan Vecerina
Guest
 
Posts: n/a
Default Re: std::stringstream && std::string

"KidLogik" <Sir_Ciph3r@nospam.com> wrote in message
news:pf2l30hcaqe0sak1pjs7k7qpf5ohct1rm0@4ax.com...
Hi,[color=blue]
> I am using std::stringstream && std::string to parse a text file ->
>
> std::ifstream in;
> std::string s;
> std::streamstring ss;[/color]
You mean stringstream I guess (and istringstream would actually be
best, once you follow the approach below).
[color=blue]
> 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;[/color]
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


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.