std::stringstream::seekg

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

  1. Dylan

    Dylan Guest

    Hi again,

    In the following program I expected step 3 to assign the values 1 and
    2 to iVal1 and iVal2 yet it appears ss.seekg(0, std::ios::beg) does
    not move the read position back to the beginning of the stream as
    expected.

    //-----------------------------------------------------------------------------
    #include <iostream>
    #include <sstream>

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

    //2. read the values 1 & 2 from the buffer and assign to iVal1 &
    iVal2
    int iVal1=-1, iVal2=-1;
    ss >> iVal1 >> iVal2;
    std::cout << "\n" << iVal1 << "," << iVal2;

    //3. now move the read position back to the beginning of the stream
    and read
    //the values again
    ss.seekg(0, std::ios::beg);
    iVal1=-1; iVal2=-1;
    ss >> iVal1 >> iVal2;
    std::cout << "\n" << iVal1 << "," << iVal2;

    //4. write the buffer to make sure it still contains "1 2" [OK]
    cout << "\n" << ss.str();

    return 0;
    }

    //-------------------------------------------------------------------------------

    output from above is:

    1,2
    1,2
    -1,-1
    1,2


    How can i move the read position back to the start of the buffer?

    thanks
     
    Dylan, Aug 11, 2005
    #1
    1. Advertising

  2. Dylan

    Guest

    You have

    std::cout << ss.tellg();

    this will print out the current read position, place it directly after
    ss.seekg(0, std::ios::beg);

    and it prints out 0
     
    , Aug 11, 2005
    #2
    1. Advertising

  3. Dylan

    Dylan Guest

    On 11 Aug 2005 04:00:55 -0700, wrote:

    >You have
    >
    >std::cout << ss.tellg();
    >
    >this will print out the current read position, place it directly after
    >ss.seekg(0, std::ios::beg);
    >
    >and it prints out 0



    so why am I not getting the expected result?
     
    Dylan, Aug 11, 2005
    #3
  4. Dylan

    Guest

    Actually no you haven't add this ss.clear(); before your ss.seekg(0,
    std::ios::beg); statement
    You have to clear the state of the stream [eofbit or failbit may have
    been set] Probably the former since you already read from the stream
    and you have to clear it before you can reset the read position.

    ss.clear();
    ss.seekg(0, std::ios::beg);

    HTH
    SMJT
     
    , Aug 11, 2005
    #4
  5. Dylan

    Dylan Guest

    On 11 Aug 2005 04:15:30 -0700, wrote:

    >Actually no you haven't add this ss.clear(); before your ss.seekg(0,
    >std::ios::beg); statement
    >You have to clear the state of the stream [eofbit or failbit may have
    >been set] Probably the former since you already read from the stream
    >and you have to clear it before you can reset the read position.
    >
    >ss.clear();
    >ss.seekg(0, std::ios::beg);
    >
    >HTH
    >SMJT


    That's it! Thanks very much
     
    Dylan, Aug 11, 2005
    #5
    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. wtnt
    Replies:
    1
    Views:
    3,038
    Jonathan Turkanis
    Jan 30, 2004
  2. KidLogik
    Replies:
    2
    Views:
    7,881
    Alberto Barbati
    Feb 24, 2004
  3. Brian  Ronk

    ifstream.seekg failing

    Brian Ronk, Oct 10, 2005, in forum: C++
    Replies:
    2
    Views:
    422
    Jim Langston
    Oct 11, 2005
  4. Tom
    Replies:
    6
    Views:
    430
    JustBoo
    Feb 11, 2006
  5. Assertor
    Replies:
    2
    Views:
    2,580
    Assertor
    Feb 26, 2006
Loading...

Share This Page