How to reuse a ostrstream?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Charles Prince

    How to reuse a ostrstream?

    How do I reuse a ostrstream?

    So far I have replaced all code that does this

    "delete <ostrstream>.st r()"

    with

    "<ostrstream>.f reeze(0)"

    to allow the <ostrstream>.dt or to perform it's house keeping.

    Now I need to reuse the ostrstream in a few places and am starting to do
    this instead of deleting and recreating a ostrstream

    <ostrstream>.fr eeze(0);
    <ostrstream>.se ekp((strlen(<os trstream>.str() ) + 1), ios::cur);

    Is this the correct way?

    I have seen this in other code when browsing google but am unsure of
    exactly what it is doing

    <ostrstream>.se ekp(0, ios::beg);

    TIA.
  • Victor Bazarov

    #2
    Re: How to reuse a ostrstream?

    "Charles Prince" <s_p_a_m_t_r_a_ p@yahoo.co.uk> wrote...[color=blue]
    > How do I reuse a ostrstream?[/color]

    I don't know how _you_ reuse it.

    Besides, why live in the past? It's been at least six years since
    *strstreams were deprecated in favour of *stringstream. Why not
    simply switch to using *stringstream?
    [color=blue]
    > [...][/color]


    Comment

    • Siemel Naran

      #3
      Re: How to reuse a ostrstream?

      "Victor Bazarov" <v.Abazarov@com Acast.net> wrote in message news:v2rpc.5321 9[color=blue]
      > "Charles Prince" <s_p_a_m_t_r_a_ p@yahoo.co.uk> wrote...[/color]
      [color=blue][color=green]
      > > How do I reuse a ostrstream?[/color][/color]

      Does o.setp(0) work? To be safe, call o.clear() first to clear the failbit
      and bitbit, in case they happen to be set.

      o.clear();
      o.setp(0);

      Upon writing a new string I think you have to insert the null char. In the
      days I used ostrstream I called o << ends; but I think o << char(0) should
      work too, though the former looks clearer.

      [color=blue]
      > I don't know how _you_ reuse it.
      >
      > Besides, why live in the past? It's been at least six years since
      > *strstreams were deprecated in favour of *stringstream. Why not
      > simply switch to using *stringstream?[/color]

      Fine, though if we we're forced to maintain legacy code or use old compilers
      and libraries, it might not be possible to make the switch.

      Anyway, the question remains. How to clear the string of an ostringstream?
      I guess you could use the method above for ostrstream (call clear and setp
      and ends) or just call o.str(""). The latter method is clearer, but it
      might reset the reserve capacity of the string to zero. Any thoughts?

      Charles, to get ostringstream include <sstream>.


      Comment

      • Victor Bazarov

        #4
        Re: How to reuse a ostrstream?

        "Siemel Naran" <SiemelNaran@RE MOVE.att.net> wrote...[color=blue]
        > "Victor Bazarov" <v.Abazarov@com Acast.net> wrote in message[/color]
        news:v2rpc.5321 9[color=blue][color=green]
        > > "Charles Prince" <s_p_a_m_t_r_a_ p@yahoo.co.uk> wrote...[/color]
        >[color=green][color=darkred]
        > > > How do I reuse a ostrstream?[/color][/color]
        >
        > Does o.setp(0) work? To be safe, call o.clear() first to clear the[/color]
        failbit[color=blue]
        > and bitbit, in case they happen to be set.
        >
        > o.clear();
        > o.setp(0);
        >
        > Upon writing a new string I think you have to insert the null char. In[/color]
        the[color=blue]
        > days I used ostrstream I called o << ends; but I think o << char(0) should
        > work too, though the former looks clearer.
        >
        >[color=green]
        > > I don't know how _you_ reuse it.
        > >
        > > Besides, why live in the past? It's been at least six years since
        > > *strstreams were deprecated in favour of *stringstream. Why not
        > > simply switch to using *stringstream?[/color]
        >
        > Fine, though if we we're forced to maintain legacy code or use old[/color]
        compilers[color=blue]
        > and libraries, it might not be possible to make the switch.
        >
        > Anyway, the question remains. How to clear the string of an[/color]
        ostringstream?[color=blue]
        > I guess you could use the method above for ostrstream (call clear and setp
        > and ends) or just call o.str(""). The latter method is clearer, but it
        > might reset the reserve capacity of the string to zero. Any thoughts?[/color]

        You surprise me. The topic of clearing an ostringstream has come up several
        times over the past couple of years. And the answer is always the same,
        use "o.str(string() ),o.clear()" (the latter operation is needed in case your
        ostringstream has been somehow violated).

        Mind you, not all library implementations correctly handle clearing of
        ostringstreams. However, that's an implementation issue.

        Victor


        Comment

        • Siemel Naran

          #5
          Re: How to reuse a ostrstream?

          "Victor Bazarov" <v.Abazarov@com Acast.net> wrote in message news:yfwpc.5610 4
          [color=blue][color=green]
          > > Anyway, the question remains. How to clear the string of an[/color]
          > ostringstream?[color=green]
          > > I guess you could use the method above for ostrstream (call clear and[/color][/color]
          setp[color=blue][color=green]
          > > and ends) or just call o.str(""). The latter method is clearer, but it
          > > might reset the reserve capacity of the string to zero. Any thoughts?[/color]
          >
          > You surprise me. The topic of clearing an ostringstream has come up[/color]
          several[color=blue]
          > times over the past couple of years. And the answer is always the same,
          > use "o.str(string() ),o.clear()" (the latter operation is needed in case[/color]
          your[color=blue]
          > ostringstream has been somehow violated).[/color]

          I wasn't reading this newsgroup for most of last year and the year before.
          As for o.str(string()) it is the same as o.str(""). However, in most
          implementations that would set the reserve capacity to zero, right?


          Comment

          • Victor Bazarov

            #6
            Re: How to reuse a ostrstream?

            "Siemel Naran" <SiemelNaran@RE MOVE.att.net> wrote...[color=blue]
            > "Victor Bazarov" <v.Abazarov@com Acast.net> wrote in message[/color]
            news:yfwpc.5610 4[color=blue]
            >[color=green][color=darkred]
            > > > Anyway, the question remains. How to clear the string of an[/color]
            > > ostringstream?[color=darkred]
            > > > I guess you could use the method above for ostrstream (call clear and[/color][/color]
            > setp[color=green][color=darkred]
            > > > and ends) or just call o.str(""). The latter method is clearer, but[/color][/color][/color]
            it[color=blue][color=green][color=darkred]
            > > > might reset the reserve capacity of the string to zero. Any thoughts?[/color]
            > >
            > > You surprise me. The topic of clearing an ostringstream has come up[/color]
            > several[color=green]
            > > times over the past couple of years. And the answer is always the same,
            > > use "o.str(string() ),o.clear()" (the latter operation is needed in case[/color]
            > your[color=green]
            > > ostringstream has been somehow violated).[/color]
            >
            > I wasn't reading this newsgroup for most of last year and the year before.[/color]

            That's what the archives are for :-)
            [color=blue]
            > As for o.str(string()) it is the same as o.str(""). However, in most
            > implementations that would set the reserve capacity to zero, right?[/color]

            It probably does. The Standard says that .str(string) deallocates the
            buffer's underlying character sequence, and copies the string's one
            there. Given an empty string, it will copy nothing. So, the buffer
            is simply deallocated. Is that somehow a bid deal (aside from needing
            some allocations in case of future outputs)?

            Victor


            Comment

            • Nobody

              #7
              Re: How to reuse a ostrstream?

              On Sat, 15 May 2004 15:54:03 +0000, Victor Bazarov wrote:
              [color=blue]
              > "Charles Prince" <s_p_a_m_t_r_a_ p@yahoo.co.uk> wrote...[color=green]
              >> How do I reuse a ostrstream?[/color]
              >
              > I don't know how _you_ reuse it.
              >
              > Besides, why live in the past? It's been at least six years since
              > *strstreams were deprecated in favour of *stringstream. Why not
              > simply switch to using *stringstream?
              >[color=green]
              >> [...][/color][/color]

              Old compiler and code and I'm a bit too scared to fix what is not broken.

              Comment

              • Nobody

                #8
                Re: How to reuse a ostrstream?

                On Sat, 15 May 2004 20:56:47 +0000, Siemel Naran wrote:
                [color=blue]
                > "Victor Bazarov" <v.Abazarov@com Acast.net> wrote in message news:v2rpc.5321 9[/color]
                <snip>[color=blue]
                > Anyway, the question remains. How to clear the string of an ostringstream?
                > I guess you could use the method above for ostrstream (call clear and setp
                > and ends) or just call o.str(""). The latter method is clearer, but it
                > might reset the reserve capacity of the string to zero. Any thoughts?
                >[/color]

                Tried o.str("") but got adverse effects so I believe you are right here.
                [color=blue]
                > Charles, to get ostringstream include <sstream>.[/color]

                Yeah thanks for the info.

                Comment

                • tom_usenet

                  #9
                  Re: How to reuse a ostrstream?

                  On Fri, 14 May 2004 10:22:03 +0100, "Charles Prince"
                  <s_p_a_m_t_r_a_ p@yahoo.co.uk> wrote:
                  [color=blue]
                  >How do I reuse a ostrstream?
                  >
                  >So far I have replaced all code that does this
                  >
                  >"delete <ostrstream>.st r()"[/color]

                  Was that delete[]? I hope so!
                  [color=blue]
                  >
                  >with
                  >
                  >"<ostrstream>. freeze(0)"
                  >
                  >to allow the <ostrstream>.dt or to perform it's house keeping.[/color]

                  That's sensible.
                  [color=blue]
                  >Now I need to reuse the ostrstream in a few places and am starting to do
                  >this instead of deleting and recreating a ostrstream
                  >
                  ><ostrstream>.f reeze(0);
                  ><ostrstream>.s eekp((strlen(<o strstream>.str( )) + 1), ios::cur);
                  >
                  >Is this the correct way?[/color]

                  No, it doesn't look like it. That is seeking way past the end of the
                  stream. I think you want:

                  <ostrstream>.cl ear(); //clear error state
                  <ostrstream>.fr eeze(false); //return memory control to stream
                  //make sure nothing is holding onto the return of str().
                  <ostrstream>.se ekp(0, ios_base::beg); //seek to start of stream

                  You need to be careful about manipulators, etc. And remember to null
                  terminate output with std::ends, or be explicit about how many
                  characters you copy from str().
                  [color=blue]
                  >I have seen this in other code when browsing google but am unsure of
                  >exactly what it is doing
                  >
                  ><ostrstream>.s eekp(0, ios::beg);[/color]

                  That's seeking to the start of the stream. This allows you to then
                  overwrite what used to be in the stream, thus "reusing" it.

                  Tom
                  --
                  C++ FAQ: http://www.parashift.com/c++-faq-lite/
                  C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

                  Comment

                  • Siemel Naran

                    #10
                    Re: How to reuse a ostrstream?

                    "Nobody" <Nobody@nowhere .com> wrote in message
                    [color=blue][color=green]
                    > > Anyway, the question remains. How to clear the string of an[/color][/color]
                    ostringstream?[color=blue][color=green]
                    > > I guess you could use the method above for ostrstream (call clear and[/color][/color]
                    setp[color=blue][color=green]
                    > > and ends) or just call o.str(""). The latter method is clearer, but it
                    > > might reset the reserve capacity of the string to zero. Any thoughts?
                    > >[/color]
                    >
                    > Tried o.str("") but got adverse effects so I believe you are right here.[/color]

                    What side effects?


                    Comment

                    Working...