std::stringstream && std::string

Discussion in 'C++' started by KidLogik, Feb 23, 2004.

  1. KidLogik

    KidLogik Guest

    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.
     
    KidLogik, Feb 23, 2004
    #1
    1. Advertising

  2. "KidLogik" <> wrote in message
    news:p...
    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
     
    Ivan Vecerina, Feb 23, 2004
    #2
    1. Advertising

  3. Ivan Vecerina wrote:
    > "KidLogik" <> wrote in message


    >>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


    By using ss.str(s) you don't even need to use "ss << s;" and it should
    also be more efficient. In that case you don't need to use a
    stringstream, an istringstream is sufficient.

    Regards,

    Alberto
     
    Alberto Barbati, Feb 24, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Giampiero Gabbiani

    std:stringstream reset

    Giampiero Gabbiani, Oct 19, 2003, in forum: C++
    Replies:
    6
    Views:
    25,426
    Giampiero Gabbiani
    Oct 19, 2003
  2. Woodster

    Clearing std::stringstream

    Woodster, Nov 17, 2003, in forum: C++
    Replies:
    2
    Views:
    7,868
    Catalin Pitis
    Nov 17, 2003
  3. Christopher Benson-Manica
    Replies:
    5
    Views:
    6,482
    Michiel Salters
    May 3, 2004
  4. Fei Liu
    Replies:
    9
    Views:
    470
  5. Grey Alien
    Replies:
    7
    Views:
    1,230
Loading...

Share This Page