471,897 Members | 1,390 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,897 software developers and data experts.

Clearing std::stringstream

I am using std::stringstream to format a string. How can I clear the
stringstream variable I am using to "re use" the same variable?

Eg:

Using std::string

std::string buffer;

buffer = "value1" + " : " + "value2";
buffer = "value3" + " : " + "value4";

Gives values of
value1 : value2
value3 : value4

The = operator for std::string overwrites the preivous contents of the
string, but I have been unable to locate a similar thing for
std:stringstream

using std::stringstream

std::stringstream buffer;

buffer << "value1" << " : " << "value2";
buffer << "value3" << " : " << "value4";

Gives values of
value1 : value2
value1 : value2value3 : value4

Which is not the desired result. How should I go about clearing the
buffer of a std::stringstream?

Thanks in advance

Jul 19 '05 #1
2 7344
Woodster wrote:
....

using std::stringstream

std::stringstream buffer;

buffer << "value1" << " : " << "value2";
buffer.str( "" ); // set the contents to the empty string.
buffer << "value3" << " : " << "value4";

Gives values of
value1 : value2
value1 : value2value3 : value4

Which is not the desired result. How should I go about clearing the
buffer of a std::stringstream?


use the .str( string ) method ... it should be an FAQ.

Jul 19 '05 #2
I'm not sure, but you can try

buffer.flush();

Catalin

"Woodster" <mi****@127.0.0.1> wrote in message
news:MP************************@news.westnet.com.a u...
I am using std::stringstream to format a string. How can I clear the
stringstream variable I am using to "re use" the same variable?

Eg:

Using std::string

std::string buffer;

buffer = "value1" + " : " + "value2";
buffer = "value3" + " : " + "value4";

Gives values of
value1 : value2
value3 : value4

The = operator for std::string overwrites the preivous contents of the
string, but I have been unable to locate a similar thing for
std:stringstream

using std::stringstream

std::stringstream buffer;

buffer << "value1" << " : " << "value2";
buffer << "value3" << " : " << "value4";

Gives values of
value1 : value2
value1 : value2value3 : value4

Which is not the desired result. How should I go about clearing the
buffer of a std::stringstream?

Thanks in advance

Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Giampiero Gabbiani | last post: by
1 post views Thread by KidLogik | last post: by
5 posts views Thread by Marcin Kalicinski | last post: by
1 post views Thread by magix | last post: by
2 posts views Thread by akitoto | last post: by
7 posts views Thread by Grey Alien | last post: by
3 posts views Thread by Rune Allnor | last post: by
reply views Thread by YellowAndGreen | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.