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
Re: How to clear out a stringstream
Quote:
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/
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
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.
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...
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.
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 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.
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;
}
Re: How to clear out a stringstream
Quote:
Originally Posted by kempofighter
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
Re: How to clear out a stringstream
Try this
std::stringstream objSS;
.............
.............
objSS.str(std::string());//Clearing