Topic: ostrstram question


Author: gozer@bcstec.ca.boeing.com (Curtis L. Green)
Date: Thu, 14 Jul 1994 19:23:49 GMT
Raw View
Dave Jones (djones@megatest.com) wrote:

: #include <iostream.h>
: #include <strstream.h>

: const int bufLen = 6;

: int main()
: {
:     char buffer[bufLen];

:     // Terminate the buffer with X followed by null.
:     buffer[bufLen-2] = 'X';
:     buffer[bufLen-1] = 0;
:
:     ostrstream ostr(buffer, bufLen);
:     ostr << "1234";
:     cout << ostr.str() << endl;
try:
      ostr << "1234" << ends;   // null terminates the buffer
      cout << buffer << endl;   // ostr is associated with array buffer
                                // normally you use ostr.str() when ostr
                                // is NOT associated with an array
:     return 0;
: }
--
-------------------------------------------------------------------------------
gozer@bcstec.ca.boeing.com |  Curtis L. Green
gozer@halcyon.com          |  777 Functional Test Software Development
second time you make it.   |  BCS support   (My opinions only.)




Author: djones@megatest.com (Dave Jones)
Date: Mon, 11 Jul 1994 22:53:35 GMT
Raw View
  Is every output operation to an ostrstream supposed to
  leave the string null terminated? I would think it should,
  but the new Sun implementation does not do that.

  I would think the following should write out "1234", but instead
  it writes out "1234X".



  -- Dave


^^^^^ snip ^^^^^ snip ^^^^^ snip ^^^^^ snip ^^^^^ snip ^^^^^ snip ^^^^^ snip

#include <iostream.h>
#include <strstream.h>

const int bufLen = 6;

int main()
{
    char buffer[bufLen];

    // Terminate the buffer with X followed by null.
    buffer[bufLen-2] = 'X';
    buffer[bufLen-1] = 0;

    ostrstream ostr(buffer, bufLen);
    ostr << "1234";
    cout << ostr.str() << endl;
    return 0;
}




Author: kitk@mudshark.sunquest.com (Kit Kauffmann)
Date: Tue, 12 Jul 1994 15:05:01
Raw View
In article <CsstKo.I1q@megatest.com> djones@megatest.com (Dave Jones) writes:

>  Is every output operation to an ostrstream supposed to
>  leave the string null terminated? I would think it should,
>  but the new Sun implementation does not do that.

>  I would think the following should write out "1234", but instead
>  it writes out "1234X".



>                -- Dave


>^^^^^ snip ^^^^^ snip ^^^^^ snip ^^^^^ snip ^^^^^ snip ^^^^^ snip ^^^^^ snip

>#include <iostream.h>
>#include <strstream.h>

>const int bufLen = 6;

>int main()
>{
>    char buffer[bufLen];

>    // Terminate the buffer with X followed by null.
>    buffer[bufLen-2] = 'X';
>    buffer[bufLen-1] = 0;
>
>    ostrstream ostr(buffer, bufLen);
>    ostr << "1234";
>    cout << ostr.str() << endl;
>    return 0;
>}

If you want a NULL, you must insert it onto the stream explicitly.  (There is
an 'ends' manipulator for this purpose.)

Later,

Kit Kauffmann   kitk@mudshark.sunquest.com (Internet)
                73363,447 (Compu$erve)
                (801) 277-5790




Author: andrewd@sfe.com.au (Andrew Davison)
Date: 12 Jul 1994 17:22:00 +1000
Raw View
djones@megatest.com (Dave Jones) writes:

>  Is every output operation to an ostrstream supposed to
>  leave the string null terminated? I would think it should,
>  but the new Sun implementation does not do that.

>  I would think the following should write out "1234", but instead
>  it writes out "1234X".



>  -- Dave


>^^^^^ snip ^^^^^ snip ^^^^^ snip ^^^^^ snip ^^^^^ snip ^^^^^ snip ^^^^^ snip

>#include <iostream.h>
>#include <strstream.h>

>const int bufLen = 6;

>int main()
>{
>    char buffer[bufLen];

>    // Terminate the buffer with X followed by null.
>    buffer[bufLen-2] = 'X';
>    buffer[bufLen-1] = 0;
>
>    ostrstream ostr(buffer, bufLen);
>    ostr << "1234" << ends;
                    ~~~~~~~~
>    cout << ostr.str() << endl;
>    return 0;
>}


No, you must output an 'ends' (i.e. end stream) see above.







Author: mikey@mcs.com (Mike Young)
Date: Tue, 12 Jul 1994 22:12:30
Raw View
In article <CsstKo.I1q@megatest.com> djones@megatest.com (Dave Jones) writes:
>From: djones@megatest.com (Dave Jones)
>Subject: ostrstram question
>Date: Mon, 11 Jul 1994 22:53:35 GMT

>  Is every output operation to an ostrstream supposed to
>  leave the string null terminated? I would think it should,
>  but the new Sun implementation does not do that.

-------------------
They're not supposed to. That's exactly the reason why your sample doesn't
behave as you expect. Try:
  ostr << 1234 << ends;

-------------------

>  I would think the following should write out "1234", but instead
>  it writes out "1234X".



>                -- Dave


>^^^^^ snip ^^^^^ snip ^^^^^ snip ^^^^^ snip ^^^^^ snip ^^^^^ snip ^^^^^ snip

>#include <iostream.h>
>#include <strstream.h>

>const int bufLen = 6;

>int main()
>{
>    char buffer[bufLen];

>    // Terminate the buffer with X followed by null.
>    buffer[bufLen-2] = 'X';
>    buffer[bufLen-1] = 0;
>
>    ostrstream ostr(buffer, bufLen);
>    ostr << "1234";
>    cout << ostr.str() << endl;
>    return 0;
>}





Author: gp@mel-comm.demon.co.uk (Guy Pickering)
Date: Wed, 13 Jul 1994 06:30:07 +0000
Raw View
In article <CsstKo.I1q@megatest.com> djones@megatest.com "Dave Jones" writes:

>  Is every output operation to an ostrstream supposed to
>  leave the string null terminated? I would think it should,
>  but the new Sun implementation does not do that.
>
>  I would think the following should write out "1234", but instead
>  it writes out "1234X".
>

No, output to string streams requires explicit NULL termination. e.g.

  ost << "Hello, my height is " << height << " metres" << ends;

'ends' provides end-of-string (or NULL termination)

Guy

--
    / Guy Pickering (Software Engineer) | MEL COMMUNICATIONS Ltd,    \
   |  email: gp@mel-comm.demon.co.uk    | Vector Point,  Newton Rd,   |
   |  Tel:   +44 (0)293 551435 X273     | Crawley,  West Sussex.      |
    \ Fax:   +44 (0)293 540045          | RH10 2PU  (United Kingdom) /