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