Connecting Tech Pros Worldwide Help | Site Map

ostringstream unexpected behaviour

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 06:07 AM
Old Wolf
Guest
 
Posts: n/a
Default ostringstream unexpected behaviour

#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, 06:07 AM
Victor Bazarov
Guest
 
Posts: n/a
Default 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


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.