Avatar of tommym121
tommym121
Flag for Canada asked on

Clear stringStream before use

When I try to use hexify multiple time, it seems to hexify the input twice.  I believe I need to clear stringstream everytime when I use it. But how? Can anyone help me?

int main() {

	string s = "My error message\n My error message\n My error message\n My error message";
	stringstream ss;

	cout << for_each(s.begin(), s.end(), hexify<char>(ss)) << endl;
	cout << for_each(s.begin(), s.end(), hexify<char>(ss)) << endl;
	// Or, as in your case:
	//
	// log4cpp::Category::getRoot() <<  log4cpp::Priority::DEBUG << for_each(s.begin(), s.end(), hexify<char>(ss)) << endl;

	return 0;
}

Open in new window

C++

Avatar of undefined
Last Comment
tommym121

8/22/2022 - Mon
SOLUTION
jkr

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
tommym121

ASKER
JKR,

How can we initialize ss with out like hexify(stringstream& out) : ss(out), .....
And then clear the string. How can that work>
SOLUTION
jkr

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
tommym121

ASKER
JKR.

Is it right ss(out) like calling fopen?  However stringstream::str() really adjust what in the pipeline, in this case, it is string stream.  Is this the right way to look at it. That is why you can initialize, before clean up the pipeline.
jkr

Sorry, but either me or you got a bit confused here.. how would 'fopen()' enter the stage?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
tommym121

ASKER
JKR,

I am sure I am the confused one and I got you confuse.  I mean ss(out) like simply attach 'out' to SS but does not copy 'out' content into ss:str().

Or maybe I should let you explain.
SOLUTION
sarabande

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
jkr

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
sarabande

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
tommym121

ASKER
Thanks