Interleaving iostream read and write

Discussion in 'C++' started by Garrett Kajmowicz, Aug 14, 2005.

  1. I have two implementations of stringstream and they both handle
    interleaved reads and writes differently. I was hoping that you might be
    able to shed some light as to the "correct" operation, and hopefully point
    out in the standard where this mode of operation is specified.

    Here is the test code:

    std::stringstream a;
    float f;
    int i;
    std::string s;

    a.str("2.35 5 Test");

    a >> f >> i >> s;

    std::cout << "f (should be 2.35): " << f << std::endl;
    std::cout << "i (should be 5): " << i << std::endl;
    std::cout << "s (should be Test): " << s << std::endl;

    a.clear();
    std::cout << "Buffer after flag clear: " << a.str() << std::endl;

    std::cout <<"Combining read and write activities\n";
    a.str("Testing data");
    s = "";
    a << 2.5 << " ";
    a >> s;
    std::cout << "Read out: " << "'" << s << "'" << std::endl;
    a >> s;
    std::cout << "Read out: " << "'" << s << "'" << std::endl;
    std::cout << "Current buffer value: " << a.str() << std::endl;


    The first sample output I get is:
    f (should be 2.35): 2.35
    i (should be 5): 5
    s (should be Test): Test
    Buffer after flag clear: 2.35 5 Test
    Combining read and write activities
    Read out: 'Testing'
    Read out: 'data2.5'
    Current buffer value: Testing data2.5

    The second sample output I get is:
    f (should be 2.35): 2.35
    i (should be 5): 5
    s (should be Test): Test
    Buffer after flag clear: 2.35 5 Test
    Combining read and write activities
    Read out: '2.5'
    Read out: 'ing'
    Current buffer value: 2.5 ing data

    Thanks for the info as to the correct operation.

    - Garrett Kajmowicz
    Garrett Kajmowicz, Aug 14, 2005
    #1
    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. John Tiger
    Replies:
    10
    Views:
    5,579
  2. ai@work
    Replies:
    9
    Views:
    538
    Ron Natalie
    Dec 16, 2004
  3. S. Nurbe

    iostream + iostream.h

    S. Nurbe, Jan 14, 2005, in forum: C++
    Replies:
    7
    Views:
    769
    red floyd
    Jan 15, 2005
  4. red floyd
    Replies:
    3
    Views:
    533
    Dietmar Kuehl
    Mar 8, 2005
  5. Tom Plunket

    any such thing as list interleaving?

    Tom Plunket, Jul 12, 2003, in forum: Python
    Replies:
    2
    Views:
    392
    Gonçalo Rodrigues
    Jul 12, 2003
Loading...

Share This Page