std:stringstream reset

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Giampiero Gabbiani

    std:stringstream reset

    Is it possible to reset a std::stringstre am in order to reuse it once more?
    I tried with flush() method without any success...
    Thanks in advance
    Giampiero

  • WW

    #2
    Re: std:stringstrea m reset

    Giampiero Gabbiani wrote:[color=blue]
    > Is it possible to reset a std::stringstre am in order to reuse it once
    > more? I tried with flush() method without any success...[/color]

    Have you tried seeking to the beinning?

    --
    WW aka Attila


    Comment

    • Giampiero Gabbiani

      #3
      Re: std:stringstrea m reset

      Il Sun, 19 Oct 2003 20:35:21 +0300, WW ha scritto:
      [color=blue]
      > Giampiero Gabbiani wrote:[color=green]
      >> Is it possible to reset a std::stringstre am in order to reuse it once
      >> more? I tried with flush() method without any success...[/color]
      >
      > Have you tried seeking to the beinning?[/color]
      Yes, I tried seekp(0), but it continues not to reset the stream.
      Actually I use this stream (a std::ostringstr eam) for writing message to
      system log, so I use the str() method in order to print the message in the stream.

      Comment

      • Gianni Mariani

        #4
        Re: std:stringstrea m reset

        Giampiero Gabbiani wrote:[color=blue]
        > Is it possible to reset a std::stringstre am in order to reuse it once more?
        > I tried with flush() method without any success...
        > Thanks in advance
        > Giampiero
        >[/color]


        The "str()" and "str( string )" methods should work.
        However, on some platforms these may not work so you
        may use the ugly reconstruct hack.


        #include <sstream>
        #include <iostream>

        int main()
        {

        std::istringstr eam iss( "Hi there" );

        std::ostringstr eam oss;

        std::string foo;

        iss >> foo;
        oss << foo;

        iss.str( "boo hoo" ); // Set the input to new string

        iss >> foo;
        oss << foo;
        std::cout << oss.str() << "\n";

        // Another way is to reconstruct the istringstream
        // UGLY but some implementations have bugs and this is the
        // only way.
        iss.~istringstr eam();
        new ( (void *) &iss ) std::istringstr eam( "Reconstruc t this" );

        oss.str( "" ); // reset the output string.

        iss >> foo;
        oss << foo;
        std::cout << oss.str() << "\n";

        // Another way is to reconstruct the ostringstream
        oss.~ostringstr eam();
        new ( (void *) &oss ) std::ostringstr eam;

        iss >> foo;
        oss << foo;
        std::cout << oss.str() << "\n";

        }

        Comment

        • Ron Natalie

          #5
          Re: std:stringstrea m reset


          "Giampiero Gabbiani" <giampiero_gabb iani@tin.it> wrote in message news:pan.2003.1 0.19.17.15.26.3 702@tin.it...[color=blue]
          > Is it possible to reset a std::stringstre am in order to reuse it once more?
          > I tried with flush() method without any success...[/color]

          Just set the string via the str(const string&) method (to either an empty string or
          one with new data as appropriate for what you are doing).


          Comment

          • Giampiero Gabbiani

            #6
            Re: std:stringstrea m reset

            Il Sun, 19 Oct 2003 17:40:09 +0000, Gianni Mariani ha scritto:
            [color=blue]
            > Giampiero Gabbiani wrote:[color=green]
            >> Is it possible to reset a std::stringstre am in order to reuse it once more?
            >> I tried with flush() method without any success...
            >> Thanks in advance
            >> Giampiero
            >>[/color]
            >
            >
            > The "str()" and "str( string )" methods should work.
            > However, on some platforms these may not work so you
            > may use the ugly reconstruct hack.
            >
            >
            > #include <sstream>
            > #include <iostream>
            >
            > int main()
            > {
            >
            > std::istringstr eam iss( "Hi there" );
            >
            > std::ostringstr eam oss;
            >
            > std::string foo;
            >
            > iss >> foo;
            > oss << foo;
            >
            > iss.str( "boo hoo" ); // Set the input to new string
            >
            > iss >> foo;
            > oss << foo;
            > std::cout << oss.str() << "\n";
            >
            > // Another way is to reconstruct the istringstream
            > // UGLY but some implementations have bugs and this is the
            > // only way.
            > iss.~istringstr eam();
            > new ( (void *) &iss ) std::istringstr eam( "Reconstruc t this" );
            >
            > oss.str( "" ); // reset the output string.
            >
            > iss >> foo;
            > oss << foo;
            > std::cout << oss.str() << "\n";
            >
            > // Another way is to reconstruct the ostringstream
            > oss.~ostringstr eam();
            > new ( (void *) &oss ) std::ostringstr eam;
            >
            > iss >> foo;
            > oss << foo;
            > std::cout << oss.str() << "\n";
            >
            > }[/color]
            Thanks a lot, I succeeded in resetting the stream with str("") method
            call!
            Bye
            Giampiero

            Comment

            • Giampiero Gabbiani

              #7
              Re: std:stringstrea m reset

              Il Sun, 19 Oct 2003 13:42:06 -0400, Ron Natalie ha scritto:
              [color=blue]
              >
              > "Giampiero Gabbiani" <giampiero_gabb iani@tin.it> wrote in message news:pan.2003.1 0.19.17.15.26.3 702@tin.it...[color=green]
              >> Is it possible to reset a std::stringstre am in order to reuse it once more?
              >> I tried with flush() method without any success...[/color]
              >
              > Just set the string via the str(const string&) method (to either an empty string or
              > one with new data as appropriate for what you are doing).[/color]
              Thanks a lot, I succeeded in resetting the stream with str("") method
              call!
              Bye
              Giampiero

              Comment

              Working...