extracting data from string streams

Discussion in 'C++' started by Dylan, Aug 10, 2005.

  1. Dylan

    Dylan Guest

    I'm attempting to write an object's state to a std::stringstream and
    then to restore that state by reading from the stringstream. The
    writing is fine but I'm having problems reading. Here's a little prog
    that demonstrates the problem:

    #include <iostream>
    #include <sstream>

    int main()
    {
    //create a buffer containing "1,2,3"
    std::stringstream ss;
    ss << 1 << "," << 2 << "," << 3;
    std::cout << "\n" << ss.str();

    //I'd like to assign 1 to iVal1 and 2 to iVal2, leaving ",3" in ss
    //(but the following doesn't work)
    int iVal1, iVal2;
    ss >> iVal1 >> iVal2;
    std::cout << "\n" << iVal1 << "," << iVal2;
    std::cout << "\n" << ss.str();

    return 0;
    }

    //The output from this is
    1,2,3
    1, -858993460
    1,2,3

    //but I'd like it to be
    1,2,3
    1,2
    3,

    How can i achieve this?

    Thanks!
    Dylan, Aug 10, 2005
    #1
    1. Advertising

  2. Dylan wrote:
    > I'm attempting to write an object's state to a std::stringstream and
    > then to restore that state by reading from the stringstream. The
    > writing is fine but I'm having problems reading. Here's a little prog
    > that demonstrates the problem:
    >
    > #include <iostream>
    > #include <sstream>
    >
    > int main()
    > {
    > //create a buffer containing "1,2,3"
    > std::stringstream ss;
    > ss << 1 << "," << 2 << "," << 3;
    > std::cout << "\n" << ss.str();
    >
    > //I'd like to assign 1 to iVal1 and 2 to iVal2, leaving ",3" in ss
    > //(but the following doesn't work)
    > int iVal1, iVal2;
    > ss >> iVal1 >> iVal2;
    > std::cout << "\n" << iVal1 << "," << iVal2;
    > std::cout << "\n" << ss.str();
    >
    > return 0;
    > }
    >
    > //The output from this is
    > 1,2,3
    > 1, -858993460
    > 1,2,3
    >
    > //but I'd like it to be
    > 1,2,3
    > 1,2
    > 3,
    >
    > How can i achieve this?


    Standart streams use whitespaces as delimeters. To achive what you want
    use spaces or tabs instead of commas when outputting.
    Maxim Yegorushkin, Aug 10, 2005
    #2
    1. Advertising

  3. Dylan

    Dylan Guest

    On 10 Aug 2005 01:58:29 -0700, "Maxim Yegorushkin"
    <> wrote:

    >
    >Dylan wrote:
    >> I'm attempting to write an object's state to a std::stringstream and
    >> then to restore that state by reading from the stringstream. The
    >> writing is fine but I'm having problems reading. Here's a little prog
    >> that demonstrates the problem:
    >>
    >> #include <iostream>
    >> #include <sstream>
    >>
    >> int main()
    >> {
    >> //create a buffer containing "1,2,3"
    >> std::stringstream ss;
    >> ss << 1 << "," << 2 << "," << 3;
    >> std::cout << "\n" << ss.str();
    >>
    >> //I'd like to assign 1 to iVal1 and 2 to iVal2, leaving ",3" in ss
    >> //(but the following doesn't work)
    >> int iVal1, iVal2;
    >> ss >> iVal1 >> iVal2;
    >> std::cout << "\n" << iVal1 << "," << iVal2;
    >> std::cout << "\n" << ss.str();
    >>
    >> return 0;
    >> }
    >>
    >> //The output from this is
    >> 1,2,3
    >> 1, -858993460
    >> 1,2,3
    >>
    >> //but I'd like it to be
    >> 1,2,3
    >> 1,2
    >> 3,
    >>
    >> How can i achieve this?

    >
    >Standart streams use whitespaces as delimeters. To achive what you want
    >use spaces or tabs instead of commas when outputting.


    ok, thanks. That helps partly, but using the >> operator doesn't
    remove the values from the stream. Do you know how I can remove
    values?
    Dylan, Aug 10, 2005
    #3
  4. Dylan wrote:
    > On 10 Aug 2005 01:58:29 -0700, "Maxim Yegorushkin"
    > <> wrote:
    >
    > >
    > >Dylan wrote:
    > >> I'm attempting to write an object's state to a std::stringstream and
    > >> then to restore that state by reading from the stringstream. The
    > >> writing is fine but I'm having problems reading. Here's a little prog
    > >> that demonstrates the problem:
    > >>
    > >> #include <iostream>
    > >> #include <sstream>
    > >>
    > >> int main()
    > >> {
    > >> //create a buffer containing "1,2,3"
    > >> std::stringstream ss;
    > >> ss << 1 << "," << 2 << "," << 3;
    > >> std::cout << "\n" << ss.str();
    > >>
    > >> //I'd like to assign 1 to iVal1 and 2 to iVal2, leaving ",3" in ss
    > >> //(but the following doesn't work)
    > >> int iVal1, iVal2;
    > >> ss >> iVal1 >> iVal2;
    > >> std::cout << "\n" << iVal1 << "," << iVal2;
    > >> std::cout << "\n" << ss.str();
    > >>
    > >> return 0;
    > >> }
    > >>
    > >> //The output from this is
    > >> 1,2,3
    > >> 1, -858993460
    > >> 1,2,3
    > >>
    > >> //but I'd like it to be
    > >> 1,2,3
    > >> 1,2
    > >> 3,
    > >>
    > >> How can i achieve this?

    > >
    > >Standart streams use whitespaces as delimeters. To achive what you want
    > >use spaces or tabs instead of commas when outputting.

    >
    > ok, thanks. That helps partly, but using the >> operator doesn't
    > remove the values from the stream. Do you know how I can remove
    > values?


    operator>> does not change the buffer, but it does move the buffer read
    pointer.
    stringstream::str() returns a copy of the whole buffer, no matter where
    the read pointer points to.

    To clear a stringstream do:

    ss.str(""); // clear the buffer
    ss.clear(); // reset stream flags
    Maxim Yegorushkin, Aug 10, 2005
    #4
    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. Eugene
    Replies:
    0
    Views:
    340
    Eugene
    Jul 30, 2003
  2. ferran
    Replies:
    5
    Views:
    500
    John Harrison
    Sep 3, 2004
  3. BigMan
    Replies:
    3
    Views:
    371
    BigMan
    Jun 12, 2005
  4. Dan

    Extracting a data from a string

    Dan, May 28, 2004, in forum: Perl Misc
    Replies:
    4
    Views:
    74
    Tore Aursand
    May 28, 2004
  5. Rene Ivon Shamberger

    extracting data from std::set<string>

    Rene Ivon Shamberger, Oct 19, 2012, in forum: C++
    Replies:
    2
    Views:
    239
Loading...

Share This Page