By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,795 Members | 1,251 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,795 IT Pros & Developers. It's quick & easy.

clearing contents of ostringstream object

P: n/a
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."
Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
news.eircom.net wrote:
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.str("");

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
*/


Jul 23 '05 #2

P: n/a
On Fri, 01 Jul 2005 13:59:24 +0400, Rolf Magnus <ra******@t-online.de>
wrote:

[]
// how do you clear the ostringstream oss?


oss.str("");


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

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

--
Maxim Yegorushkin
<fi****************@gmail.com>
Jul 23 '05 #3

P: n/a
"Maxim Yegorushkin" <fi****************@gmail.com> wrote in message

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

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


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
Jul 23 '05 #4

P: n/a
On Fri, 01 Jul 2005 22:45:54 +0400, Kyle Kolander <kk*******@hotmail.com>
wrote:

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


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
<fi****************@gmail.com>
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.