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

  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. 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. Mark
    Replies:
    4
    Views:
    2,961
    scoude
    Jan 12, 2011
  2. Victor Bazarov
    Replies:
    0
    Views:
    852
    Victor Bazarov
    Jun 25, 2003
  3. Russell Hanneken
    Replies:
    0
    Views:
    898
    Russell Hanneken
    Jun 25, 2003
  4. Steven Van den Berghe

    unexpected map behaviour

    Steven Van den Berghe, Aug 28, 2003, in forum: C++
    Replies:
    2
    Views:
    426
    Christian Jan├čen
    Aug 28, 2003
  5. Simon Pryor
    Replies:
    5
    Views:
    1,125
    Peter Kragh
    Jun 17, 2004
Loading...

Share This Page