How to clear out a stringstream
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Thread: How to clear out a stringstream

  1. #1
    Join Date
    Jan 2003
    Posts
    155

    How to clear out a stringstream

    Hello,

    I thought the following code would work, but alas I am stumped. I find little docs on this on the net, and was hoping a member here had a solution other than 'ungetting' all characters gotten.


    Code:
    	stringstream a;
    
    	for( int i = 0 ; i < 10 ; i++ ){
    		a.flush();
                                    a.clear();
    		a << i ;
    		cout << a.str();
    	}
    Flush or Clear do not seem to do anything, the idea is to clear it to receive a new slate for the upcoming elements. The code above is simplistic, but I hope I am making sense.

    Thanks.
    Alex

  2. #2
    Join Date
    Jun 2003
    Location
    Gjøvik, Norway
    Posts
    204
    The obious thing to do, would be to simply move the definition of a into the for-loop's curlies. That way it will be constructed/destructed each iteration. (Giving you an empty stream each time)

    That doesn't however answer the actual question. I would actually also like to know if this can be done easily as I have had the same problem myself, and couldn't dig up anything on the matter.

    EDIT: Typo
    Last edited by Assmaster; April 26th, 2004 at 05:44 PM.

  3. #3
    Join Date
    Feb 2003
    Posts
    377
    The best way is probably to use str(""):
    Code:
    #include <iostream>
    #include <sstream>
    using namespace std;
    int main()
    {
        stringstream a;
    
        for( int i = 0 ; i < 10 ; i++ ){
            a.str("");
            a << i ;
            cout << a.str();
        }
    }
    You could also use seekp(0), but that won't clear the buffer, it will just insert the next value at the beginning.

  4. #4
    Join Date
    Jan 2003
    Posts
    155
    Thank you jlou.

  5. #5
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: How to clear out a stringstream

    Originally posted by Saeven
    Flush or Clear do not seem to do anything
    flush() flushes the stream, e.g. writes the data on the screen or into file. For a stringstream it really doesn't do anything.
    clear() is inherited from ios and clears the status flags (resets them to good). Not sure if stringstreams can have other state but good but anyways.
    jlou's suggestion is the only one I know off. There are not many member functions for ostringstream and as clearing the stream is something specific to stringstream (i.e. I don't expect ostream to have a member function for that) I guess that's the way to go.
    BTW, I am usually using the following site if it comes to looking up istream library related stuff:
    http://www.cplusplus.com/ref/

  6. #6
    Join Date
    Oct 2008
    Location
    Brandenburg an der Havel, Germany
    Posts
    2

    Re: How to clear out a stringstream

    consider this code.

    std::stringstream ss;
    std::string str1, str2;
    float f1 = 10.0;
    float f2 = 20.0;

    ss << f1;
    ss >> str1;

    ss.str("");

    ss << f2;
    ss >> str2;

    you'll find that str2 is empty.

    ss.str("") clears the stream but when you try to add a new value to the stream, it somehow doesnt get updated. therefore, you have to add a clear() statement just before or after str("") like the following...

    ss.clear();
    ss.str("");

    or

    ss.str("");
    ss.clear();

    this not only clears the stream but also allows you to add a new value to it.

    Vaasan
    Last edited by vaasan; October 10th, 2008 at 11:43 AM.

  7. #7
    Join Date
    Feb 2003
    Posts
    377

    Re: How to clear out a stringstream

    I'm not sure what the policy on bumping old threads is here, but this one is over four and a half years old.

    The clear() just clears the failbit and/or eofbit (and the badbit, too, although that is unlikely to be set). In the case of the OP, the failbit and eofbit are never set in the loop, so the clear is not necessary. It wouldn't hurt, but it wouldn't help here. You can still add a new value.

    If you use the extraction operator>> with a stringstream, then you might need to use clear() to allow reading from the stream again (as opposed to writing to it). That's because if you read from the stream it might set the failbit or eofbit and it won't let you read again until you clear those bits.

  8. #8
    Join Date
    Aug 2007
    Posts
    858

    Re: How to clear out a stringstream

    Wouldn't you need to do

    Code:
    stringstream foo;
    
    foo.str( ).clear( );
    ?

    But then, I'm a bit rusty on stringstreams...

  9. #9
    Join Date
    Feb 2003
    Posts
    377

    Re: How to clear out a stringstream

    That would just empty out the temporary string returned by the stringstream (if it was allowed at all since the string would be temporary). It wouldn't have any effect on the stream itself or the program.

    A container class's clear() function (like what you used) is different than a stream class's clear() function.

  10. #10
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Re: How to clear out a stringstream

    Quote Originally Posted by vaasan
    consider this code
    < SNIP >
    Vaasan
    I considered that block of code. In fact I compiled and ran it and it crashed. at the line I highlighted in the quoted code block.
    Code:
    #include <iostream>
    #include <sstream>
    #include <string>
    int main(int argc, char *argv[])
    {
    
        std::stringstream ss;
        std::string str1, str2;
        float f1 = 10.0;
        float f2 = 20.0;
    
        ss << f1;
        std::cout << ss.str() << std::endl;
    
        // this line crashes
         ss >> str1;
    
        ss.str("");
    
        ss << f2;
        ss >> str2;
        int input;
        std::cin >> input;
        return 0;
    }
    something seems seriously wrong with that example so it doesn't surprise me that you are seeing weird behavior. I've used the
    Code:
    ss.str("");
    method for clearing out a string stream in the past and was able to make it work. I'm not sure I understand what else is wrong with that example off the top of my head. Seems like the stream operator is failing to insert data into the string. I'm not sure if you can do it like that.
    Last edited by kempofighter; October 10th, 2008 at 05:19 PM.

  11. #11
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Re: How to clear out a stringstream

    This seems to work ok.
    Code:
    int main(int argc, char *argv[])
    {
        std::stringstream ss;
        std::string str1, str2;
        float f1 = 10.0;
        float f2 = 20.0;
    
        ss << f1;
        
        str1 = ss.str();
        std::cout << "str1: " << str1 << std::endl;
    
        ss.str("");
    
        ss << f2;
        str2 = ss.str();
        std::cout << "str2: " << str2 << std::endl;
        int input;
        std::cin >> input;
        return 0;
    }
    Last edited by kempofighter; October 10th, 2008 at 05:22 PM. Reason: tags

  12. #12
    Join Date
    Oct 2008
    Location
    Brandenburg an der Havel, Germany
    Posts
    2

    Re: How to clear out a stringstream

    Quote Originally Posted by kempofighter
    This seems to work ok.
    Code:
    <snippet>
    Thank you for all your inputs. Somehow this snippet doesnt work for me. Only when I use both ss.clear() and ss.str("") together, I am able to fully flush the stringstream. Or else the last assigned value is still retained in it.

    Also the mentioned line

    ss >> str1;

    doesnt crash for me. I am wondering why. Compiler differences?

    vaasan
    Last edited by vaasan; October 11th, 2008 at 11:43 AM. Reason: snippet was removed

  13. #13
    Join Date
    May 2009
    Posts
    1

    Re: How to clear out a stringstream

    Try this

    std::stringstream objSS;

    .............
    .............

    objSS.str(std::string());//Clearing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center