std::stringstream && std::string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • KidLogik

    #1

    std::stringstream && std::string

    Hello!

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

    std::ifstream in;
    std::string s;
    std::streamstri ng 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::stringstre am because it makes extracting data easier
    like so -> ss >> n;

    Is this the proper way to use std::stringstre am? How do I blank out
    the std::stringstre am 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.

  • Ivan Vecerina

    #2
    Re: std::stringstre am &amp;&amp; std::string

    "KidLogik" <Sir_Ciph3r@nos pam.com> wrote in message
    news:pf2l30hcaq e0sak1pjs7k7qpf 5ohct1rm0@4ax.c om...
    Hi,[color=blue]
    > I am using std::stringstre am && std::string to parse a text file ->
    >
    > std::ifstream in;
    > std::string s;
    > std::streamstri ng 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::istringstr eam 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


    Comment

    Working...