Connecting Tech Pros Worldwide Forums | Help | Site Map

stringstream problem

Bill Beacom
Guest
 
Posts: n/a
#1: Jul 19 '05
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

Buster
Guest
 
Posts: n/a
#2: Jul 19 '05

re: stringstream problem


> ....but I ahve not found a successful way to[color=blue]
> "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[/color]

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

Regards,
Buster.


Peter Kragh
Guest
 
Posts: n/a
#3: Jul 19 '05

re: stringstream problem



"Buster" <noone@nowhere.com> wrote in message
news:bk50cl$tc9$1@newsg2.svr.pol.co.uk...[color=blue][color=green]
> > ....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[/color]
>
> ssMsg.str (""); // this way is correct.[/color]

or

ssMsg.str(string());
[color=blue]
>
> Regards,
> Buster.
>
>[/color]

BR,
Peter Kragh


Closed Thread