stringstream problem

B

Bill Beacom

Hi all,

I am trying to use stringstream to assemble a text message from a set
of user-defined objects that have the insertion operator overloaded.
It seems to be putting them into the stringstream fine....however, I
need to assemble it in "parts" (eg a prefix, a header, a body, a
trailer and a checksum), and would like to reuse the stringstream to
format that various parts....but I ahve not found a successful way to
"empty" the stringstream object after one set of insertion operations.
The code needs to work on both VC++ 6 and UNIX (Solaris) patforms. I
have tried something like this:

stringstream ssMsg;
string strHeader;
string strBody;

ssMsg << m_Header; // format the header object as a string
strHeader = ssMsg.str();

ssMsg.flush() // <--- one attempt to "empty" the stringstream....didnt
work
// also tried ssMsg.str("") which seemed to 'work' in
VC++ 6

ssMsg << m_Body; // <-- on UNIX this now has header & body
strHeader = ssMSg.str(); // <-- so, this now has header and body when
I want only the body...

....
etc

would using the string extraction operator help?
eg

ssMsg >> strHeader;

The stream MAY have embedded spaces and/or newlines...

Comments on approach also welcome....

Thanks in advance.

Bill
 
B

Buster

....but I ahve not found a successful way to
"empty" the stringstream object after one set of insertion operations.

ssMsg.flush() // <--- one attempt to "empty" the stringstream...
// also tried ssMsg.str("") which seemed to 'work' in VC++ 6

ssMsg.str (""); // this way is correct.

Regards,
Buster.
 

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,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top