stringstream problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bill Beacom

    stringstream problem

    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

    #2
    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.


    Comment

    • Peter Kragh

      #3
      Re: stringstream problem


      "Buster" <noone@nowhere. com> wrote in message
      news:bk50cl$tc9 $1@newsg2.svr.p ol.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(strin g());
      [color=blue]
      >
      > Regards,
      > Buster.
      >
      >[/color]

      BR,
      Peter Kragh


      Comment

      Working...