Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > ostringstream unexpected behaviour

Reply
Thread Tools

ostringstream unexpected behaviour

 
 
Old Wolf
Guest
Posts: n/a
 
      02-04-2004
#include <iostream>
#include <ostream>
#include <sstream>

int main(void)
{
std: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?
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      02-04-2004
"Old Wolf" <(E-Mail Removed)> wrote...
> #include <iostream>
> #include <ostream>
> #include <sstream>
>
> int main(void)
> {
> std: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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unexpected behaviour for dispatching events... Mark Java 4 01-12-2011 04:21 PM
std::ostringstream bizarre behaviour. Please help! Simon Pryor C++ 5 06-17-2004 10:48 PM
unexpected map behaviour Steven Van den Berghe C++ 2 08-28-2003 11:59 AM
Re: std::ostringstream unexpected behavior with .net 2003. Russell Hanneken C++ 0 06-25-2003 10:22 PM
Re: std::ostringstream unexpected behavior with .net 2003. Victor Bazarov C++ 0 06-25-2003 10:20 PM



Advertisments