ostringstream unexpected behaviour

Discussion in 'C++' started by Old Wolf, Feb 4, 2004.

  1. Old Wolf

    Old Wolf Guest

    #include <iostream>
    #include <ostream>
    #include <sstream>

    int main(void)
    {
    std::eek:stringstream oss;

    oss << "foo";

    oss.str("APP");
    std::cout << oss.str() << '\n';
    oss << "END";
    std::cout << oss.str() << '\n';

    oss.str("");
    oss << "APP";
    std::cout << oss.str() << '\n';
    oss << "END";
    std::cout << oss.str() << std::endl;

    return 0;
    }

    I expected to see the output:

    APP
    APPEND
    APP
    APPEND

    but the second line said "END" instead. Why is this?
    What is the best way to set the string in an ostringstream,
    and leave it in a usable state?
     
    Old Wolf, Feb 4, 2004
    #1
    1. Advertisements

  2. "Old Wolf" <> wrote...
    > #include <iostream>
    > #include <ostream>
    > #include <sstream>
    >
    > int main(void)
    > {
    > std::eek:stringstream oss;
    >
    > oss << "foo";
    >
    > oss.str("APP");
    > std::cout << oss.str() << '\n';
    > oss << "END";
    > std::cout << oss.str() << '\n';
    >
    > oss.str("");
    > oss << "APP";
    > std::cout << oss.str() << '\n';
    > oss << "END";
    > std::cout << oss.str() << std::endl;
    >
    > return 0;
    > }
    >
    > I expected to see the output:
    >
    > APP
    > APPEND
    > APP
    > APPEND
    >
    > but the second line said "END" instead. Why is this?
    > What is the best way to set the string in an ostringstream,
    > and leave it in a usable state?


    Open the ostringstream for append (see ios_base::app).

    Victor
     
    Victor Bazarov, Feb 4, 2004
    #2
    1. Advertisements

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. Mark
    Replies:
    4
    Views:
    3,120
    scoude
    Jan 12, 2011
  2. Steven Van den Berghe

    unexpected map behaviour

    Steven Van den Berghe, Aug 28, 2003, in forum: C++
    Replies:
    2
    Views:
    553
    Christian Janßen
    Aug 28, 2003
  3. Simon Pryor
    Replies:
    5
    Views:
    1,316
    Peter Kragh
    Jun 17, 2004
  4. Ioannis Vranos

    Unexpected behaviour

    Ioannis Vranos, Sep 23, 2004, in forum: C++
    Replies:
    36
    Views:
    1,084
    Rolf Magnus
    Sep 24, 2004
  5. Richard Philips

    Unexpected python behaviour

    Richard Philips, Nov 28, 2003, in forum: Python
    Replies:
    2
    Views:
    417
    Jay O'Connor
    Nov 28, 2003
  6. Simon Wittber

    Unexpected mod-python behaviour.

    Simon Wittber, Feb 4, 2004, in forum: Python
    Replies:
    1
    Views:
    371
    Bengt Richter
    Feb 6, 2004
  7. Riccardo Galli

    curses : unexpected behaviour with pad

    Riccardo Galli, Sep 6, 2004, in forum: Python
    Replies:
    1
    Views:
    525
  8. Steven D'Aprano
    Replies:
    8
    Views:
    464
    Christos Georgiou
    Feb 16, 2006
Loading...