clearing contents of ostringstream object

  • Thread starter news.eircom.net
  • Start date
N

news.eircom.net

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

Rolf Magnus

news.eircom.net said:
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
*/
 
K

Kyle Kolander

Maxim Yegorushkin said:
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
 
M

Maxim Yegorushkin

[]
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top