stringstream

M

Marc Schellens

is there an easy way to clear a stringstreams contents.
My problem is that I want output to be limited to 80 characters
a line. So I use operator<< to a stringstream object 'oss' and
if tellp() is > 80, I use operator<< to output
oss.rdbuf()->str();
Afterwards I want to clear oss contents. Is there a way.
pubseekpos(0) seems not to delete the former contents.
Is there maybe a more elegant way to archieve what I want?
I find 'oss.rdbuf()->str()' already to complicated.

cout << oss; oss.clear();

would look much nicer.

thanks,
marc
 
M

Marc Schellens

Thanks,
thats exactly what I wanted to know.
But as we are on it:
How can I check if there is already an 'endl' inserted in an
ostringstream ?
My tries with pubseekpos(...) weren't successful so far...
Do I need a stringstream then?

thanks,
marc
 
S

Sean

Marc Schellens said:
is there an easy way to clear a stringstreams contents.
My problem is that I want output to be limited to 80 characters
a line. So I use operator<< to a stringstream object 'oss' and
if tellp() is > 80, I use operator<< to output
oss.rdbuf()->str();
Afterwards I want to clear oss contents. Is there a way.
pubseekpos(0) seems not to delete the former contents.
Is there maybe a more elegant way to archieve what I want?
I find 'oss.rdbuf()->str()' already to complicated.

cout << oss; oss.clear();

would look much nicer.

thanks,
marc

cout << oss.str() ; oss.str("")

Cheers,
Sean
 
S

Sean

Marc Schellens said:
Thanks,
thats exactly what I wanted to know.
But as we are on it:
How can I check if there is already an 'endl' inserted in an
ostringstream ?
My tries with pubseekpos(...) weren't successful so far...
Do I need a stringstream then?

thanks,
marc

Marc,

Not really sure what you are looking for but
perhaps this will help :

string ms(oss.str());
int elp = ms.find("\n");
if (elp == string::npos)
cout << ms; // no endl found
else cout << ms.substr(0,elp); // endl found, show substring

HTH,
Sean
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top