Connecting Tech Pros Worldwide Forums | Help | Site Map

clearing contents of ostringstream object

news.eircom.net
Guest
 
Posts: n/a
#1: Jul 23 '05
Hello,

How do you clear the contents of an ostringstream object? I've included
some example code so you can see what I mean.

t.i.a.
Craig H.


#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>

using namespace std;

int main(int argc, char* argv[])
{
string a("hello");
string b("world");
string s1;
ostringstream oss;

oss << setiosflags(ios::left) << setfill('O') << setw(10) << a << endl;
s1 = oss.str();
cout << s1;

// how do you clear the ostringstream oss?

oss << resetiosflags(ios::left) << setfill('W') << setw(10) << b << endl;
s1 = oss.str();
cout << s1;

system("PAUSE");
return EXIT_SUCCESS;
}

/*
-curent output-
helloOOOOO
helloOOOOO
WWWWWworld

-desired output-
helloOOOOO
WWWWWworld
*/



--
"Communism won't work because people like to own stuff."

Rolf Magnus
Guest
 
Posts: n/a
#2: Jul 23 '05

re: clearing contents of ostringstream object


news.eircom.net wrote:
[color=blue]
> Hello,
>
> How do you clear the contents of an ostringstream object?
> I've included some example code so you can see what I mean.
>
> t.i.a.
> Craig H.
>
>
> #include <iostream>
> #include <string>
> #include <sstream>
> #include <iomanip>
>
> using namespace std;
>
> int main(int argc, char* argv[])
> {
> string a("hello");
> string b("world");
> string s1;
> ostringstream oss;
>
> oss << setiosflags(ios::left) << setfill('O') << setw(10) << a << endl;
> s1 = oss.str();
> cout << s1;
>
> // how do you clear the ostringstream oss?[/color]

oss.str("");
[color=blue]
>
> oss << resetiosflags(ios::left) << setfill('W') << setw(10) << b << endl;
> s1 = oss.str();
> cout << s1;
>
> system("PAUSE");
> return EXIT_SUCCESS;
> }
>
> /*
> -curent output-
> helloOOOOO
> helloOOOOO
> WWWWWworld
>
> -desired output-
> helloOOOOO
> WWWWWworld
> */
>
>
>[/color]

Maxim Yegorushkin
Guest
 
Posts: n/a
#3: Jul 23 '05

re: clearing contents of ostringstream object


On Fri, 01 Jul 2005 13:59:24 +0400, Rolf Magnus <ramagnus@t-online.de>
wrote:

[]
[color=blue][color=green]
>> // how do you clear the ostringstream oss?[/color]
>
> oss.str("");[/color]

Do not forget to reset flags. The complete clear is:

oss.str("");
oss.clear();

--
Maxim Yegorushkin
<firstname.lastname@gmail.com>
Kyle Kolander
Guest
 
Posts: n/a
#4: Jul 23 '05

re: clearing contents of ostringstream object


"Maxim Yegorushkin" <firstname.lastname@gmail.com> wrote in message[color=blue]
>
> Do not forget to reset flags. The complete clear is:
>
> oss.str("");
> oss.clear();
>[/color]

From looking at sections 27.7.3.2 and 27.7.1.2 of the standard, it appears
str("") does not clear the stream bits. It seems to me that in clearing the
internal buffer, one would intend that the stream bits would also be reset
(in a good state). Is there a reason this is not the defined behavior?
Aside from a slight performance hit, I have not been able to think of a
situation where it would be beneficial to set the internal buffer to an
empty string (in essence resetting it), but leave the state of the stream
unchanged. If performance is the reason, then under what conditions will an
ostringstream enter a "bad" state (needing to be clear()'ed)?

If I'm just doing stuff like this, is there ever a need to clear():

// contrived example
ostringstream oss;
for (int i = 0; i < 50; ++i)
{
oss << "File:" << i;
cout << oss.str() << endl;
oss.str("");
}

Thanks,
Kyle


Maxim Yegorushkin
Guest
 
Posts: n/a
#5: Jul 23 '05

re: clearing contents of ostringstream object


On Fri, 01 Jul 2005 22:45:54 +0400, Kyle Kolander <kkolander@hotmail.com>
wrote:

[]
[color=blue]
> If performance is the reason, then under what conditions will an
> ostringstream enter a "bad" state (needing to be clear()'ed)?[/color]

Consider:

#include <iostream>
#include <sstream>

int main()
{
using namespace std;

stringstream ss;

cout << ss.rdstate() << endl;

ss.str("a");
int a;
ss >> a;

cout << ss.rdstate() << endl;
}

outputs:
0
4

--
Maxim Yegorushkin
<firstname.lastname@gmail.com>
Closed Thread


Similar C / C++ bytes