Connecting Tech Pros Worldwide Help | Site Map

ostringstream unexpected behaviour

Old Wolf
Guest
 
Posts: n/a
#1: Jul 22 '05
#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?
Victor Bazarov
Guest
 
Posts: n/a
#2: Jul 22 '05

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 C / C++ bytes