Connecting Tech Pros Worldwide Help | Site Map

ostringstream unexpected behaviour

  #1  
Old July 22nd, 2005, 07:07 AM
Old Wolf
Guest
 
Posts: n/a
#include <iostream>
#include <ostream>
#include <sstream>

int main(void)
{
std::ostringstream 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?
  #2  
Old July 22nd, 2005, 07:07 AM
Victor Bazarov
Guest
 
Posts: n/a

re: ostringstream unexpected behaviour


"Old Wolf" <oldwolf@inspire.net.nz> wrote...[color=blue]
> #include <iostream>
> #include <ostream>
> #include <sstream>
>
> int main(void)
> {
> std::ostringstream 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?[/color]

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

Victor


Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
ostream,stringstream and char* Protazy answers 6 September 2nd, 2006 09:55 AM