how to clear a stringstream object's data, not state.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Àî°×

    how to clear a stringstream object's data, not state.

    hi all:

    I want erase a stringstream' content for input new data to it.

    example:
    std::stringstre am stm;
    stm<<"this is a string";

    std::cout<<stm. str();
    // here print:this is a string

    // do something here of the stm, i don't know .....
    stm<<"not a string";

    std::cout<<strm .str();
    // I want print "not a string", not "this is a string not a string"


  • Rolf Magnus

    #2
    Re: how to clear a stringstream object's data, not state.

    Àî°× wrote:
    [color=blue]
    > hi all:
    >
    > I want erase a stringstream' content for input new data to it.
    >
    > example:
    > std::stringstre am stm;
    > stm<<"this is a string";
    >
    > std::cout<<stm. str();
    > // here print:this is a string
    >
    > // do something here of the stm, i don't know .....[/color]

    stm.str("");
    [color=blue]
    > stm<<"not a string";
    >
    > std::cout<<strm .str();
    > // I want print "not a string", not "this is a string not a string"[/color]

    Comment

    • Àî°×

      #3
      Re: how to clear a stringstream object's data, not state.

      this is not a complete method. need a operate like a basic_stirng::e rase().
      not only stringstream, also fstream etc.
      "Rolf Magnus" <ramagnus@t-online.de> дÈëÏûÏ¢ÐÂÎÅ:e1 fr0i$sha$01$1@n ews.t-online.com...[color=blue]
      > wrote:
      >[color=green]
      >> hi all:
      >>
      >> I want erase a stringstream' content for input new data to it.
      >>
      >> example:
      >> std::stringstre am stm;
      >> stm<<"this is a string";
      >>
      >> std::cout<<stm. str();
      >> // here print:this is a string
      >>
      >> // do something here of the stm, i don't know .....[/color]
      >
      > stm.str("");
      >[color=green]
      >> stm<<"not a string";
      >>
      >> std::cout<<strm .str();
      >> // I want print "not a string", not "this is a string not a string"[/color]
      >[/color]


      Comment

      • Rolf Magnus

        #4
        Re: how to clear a stringstream object's data, not state.

        Àî°× wrote:
        [color=blue]
        > this is not a complete method.[/color]

        It does exactly what you asked for. It clears a stringstream's content.
        [color=blue]
        > need a operate like a basic_stirng::e rase(). not only stringstream, also
        > fstream etc.[/color]

        Well, you didn't mention fstream until now, so how could I have known this?
        Anyway, what do you want it to do for an fstream? Remove the file? Replace
        it with an empty file?

        Comment

        • Àî°×

          #5
          Re: how to clear a stringstream object's data, not state.

          a function can clear all stream of the basic_ios object , fstream,
          stringstream, wstringstream.. .
          "Rolf Magnus" <ramagnus@t-online.de> дÈëÏûÏ¢ÐÂÎÅ:e1 fvkn$3an$00$2@n ews.t-online.com...[color=blue]
          > wrote:
          >[color=green]
          >> this is not a complete method.[/color]
          >
          > It does exactly what you asked for. It clears a stringstream's content.
          >[color=green]
          >> need a operate like a basic_stirng::e rase(). not only stringstream, also
          >> fstream etc.[/color]
          >
          > Well, you didn't mention fstream until now, so how could I have known
          > this?
          > Anyway, what do you want it to do for an fstream? Remove the file? Replace
          > it with an empty file?
          >[/color]


          Comment

          • Àî°×

            #6
            Re: how to clear a stringstream object's data, not state.

            a algrithom in stl, the basic_ios not iterator
            "Rolf Magnus" <ramagnus@t-online.de> дÈëÏûÏ¢ÐÂÎÅ:e1 fvkn$3an$00$2@n ews.t-online.com...[color=blue]
            > wrote:
            >[color=green]
            >> this is not a complete method.[/color]
            >
            > It does exactly what you asked for. It clears a stringstream's content.
            >[color=green]
            >> need a operate like a basic_stirng::e rase(). not only stringstream, also
            >> fstream etc.[/color]
            >
            > Well, you didn't mention fstream until now, so how could I have known
            > this?
            > Anyway, what do you want it to do for an fstream? Remove the file? Replace
            > it with an empty file?
            >[/color]


            Comment

            • Daniel T.

              #7
              Re: how to clear a stringstream object's data, not state.

              In article <e1fsn5$hp0$1@n ews.cn99.com>, "Àî°×" <zhfxa@163.co m> wrote:
              [color=blue]
              > hi all:
              >
              > I want erase a stringstream' content for input new data to it.
              >
              > example:
              > std::stringstre am stm;
              > stm<<"this is a string";
              >
              > std::cout<<stm. str();
              > // here print:this is a string
              >
              > // do something here of the stm, i don't know .....
              > stm<<"not a string";
              >
              > std::cout<<strm .str();
              > // I want print "not a string", not "this is a string not a string"[/color]
              [color=blue]
              > need a operate like a basic_stirng::e rase(). not only stringstream,
              > also fstream etc.[/color]
              [color=blue]
              > a algrithom in stl, the basic_ios not iterator[/color]
              [color=blue]
              > a function can clear all stream of the basic_ios object , fstream,
              > stringstream, wstringstream.. .[/color]

              You still haven't made it clear what you want. basic_ios objects don't
              have a str() function so they wouldn't work in the sample code you
              provided.

              To clear an istream, use "ignore()".

              What would it mean to clear an ostream? How would you know it's cleared?


              --
              Magic depends on tradition and belief. It does not welcome observation,
              nor does it profit by experiment. On the other hand, science is based
              on experience; it is open to correction by observation and experiment.

              Comment

              • Tom Widmer

                #8
                Re: how to clear a stringstream object's data, not state.

                Àî°× wrote:[color=blue]
                > a function can clear all stream of the basic_ios object , fstream,
                > stringstream, wstringstream.. .[/color]

                What do you mean by "clear"? What would it mean to clear a printer? In
                general, streams cannot be cleared.

                Tom

                Comment

                • BobR

                  #9
                  Re: how to clear a stringstream object's data, not state.


                  Daniel T. wrote in message ...[color=blue]
                  >In article <e1fsn5$hp0$1@n ews.cn99.com>, "Àî°×" <zhfxa@163.co m> wrote:
                  >[color=green]
                  >> hi all:
                  >> I want erase a stringstream' content for input new data to it.
                  >> example:
                  >> std::stringstre am stm;
                  >> stm<<"this is a string";
                  >> std::cout<<stm. str(); // here print:this is a string
                  >> // do something here of the stm, i don't know .....
                  >> stm<<"not a string";
                  >> std::cout<<strm .str();
                  >> // I want print "not a string", not "this is a string not a string"[/color]
                  >[color=green]
                  >> need a operate like a basic_stirng::e rase(). not only stringstream,
                  >> also fstream etc.
                  >> a algrithom in stl, the basic_ios not iterator
                  >> a function can clear all stream of the basic_ios object , fstream,
                  >> stringstream, wstringstream.. .[/color]
                  >
                  >You still haven't made it clear what you want. basic_ios objects don't
                  >have a str() function so they wouldn't work in the sample code you
                  >provided.
                  >
                  >To clear an istream, use "ignore()".
                  >
                  >What would it mean to clear an ostream? How would you know it's cleared?[/color]


                  Add:
                  OP("Àî°×"), try this:

                  // #includes here
                  int main(){
                  std::ostringstr eam Oss; // note type of stream.

                  Oss<<"this is a string";
                  std::cout<<Oss. str(); // here print: 'this is a string'

                  Oss.str(""); // empty the ostringstream object buffer.
                  // Oss.clear(); // if stream was in fail state.

                  // do something here of the stm, i don't know .....
                  Oss<<"not a string";
                  std::cout<<Oss. str();

                  return 0;
                  } // main() end

                  Is that what you ("Àî°×") were looking for?

                  --
                  Bob R
                  POVrookie


                  Comment

                  • Daniel T.

                    #10
                    Re: how to clear a stringstream object's data, not state.

                    In article <pQX_f.11592$YT 1.208@bgtnsc04-news.ops.worldn et.att.net>,
                    "BobR" <RemoveBadBobR@ worldnet.att.ne t> wrote:
                    [color=blue]
                    > Daniel T. wrote in message ...[color=green]
                    > >In article <e1fsn5$hp0$1@n ews.cn99.com>, "Àî°×" <zhfxa@163.co m> wrote:
                    > >[color=darkred]
                    > >> hi all:
                    > >> I want erase a stringstream' content for input new data to it.
                    > >> example:
                    > >> std::stringstre am stm;
                    > >> stm<<"this is a string";
                    > >> std::cout<<stm. str(); // here print:this is a string
                    > >> // do something here of the stm, i don't know .....
                    > >> stm<<"not a string";
                    > >> std::cout<<strm .str();
                    > >> // I want print "not a string", not "this is a string not a string"[/color]
                    > >[color=darkred]
                    > >> need a operate like a basic_stirng::e rase(). not only stringstream,
                    > >> also fstream etc.
                    > >> a algrithom in stl, the basic_ios not iterator
                    > >> a function can clear all stream of the basic_ios object , fstream,
                    > >> stringstream, wstringstream.. .[/color]
                    > >
                    > >You still haven't made it clear what you want. basic_ios objects don't
                    > >have a str() function so they wouldn't work in the sample code you
                    > >provided.
                    > >
                    > >To clear an istream, use "ignore()".
                    > >
                    > >What would it mean to clear an ostream? How would you know it's cleared?[/color]
                    >
                    >
                    > Add:
                    > OP("Àî°×"), try this:
                    >
                    > // #includes here
                    > int main(){
                    > std::ostringstr eam Oss; // note type of stream.
                    >
                    > Oss<<"this is a string";
                    > std::cout<<Oss. str(); // here print: 'this is a string'
                    >
                    > Oss.str(""); // empty the ostringstream object buffer.
                    > // Oss.clear(); // if stream was in fail state.
                    >
                    > // do something here of the stm, i don't know .....
                    > Oss<<"not a string";
                    > std::cout<<Oss. str();
                    >
                    > return 0;
                    > } // main() end
                    >
                    > Is that what you ("Àî°×") were looking for?[/color]

                    He's already said he wants something that will work with any stream, not
                    just stringstreams.


                    --
                    Magic depends on tradition and belief. It does not welcome observation,
                    nor does it profit by experiment. On the other hand, science is based
                    on experience; it is open to correction by observation and experiment.

                    Comment

                    Working...