Skip to main content
GameDev.net gamedev.net
🔒 Locked

clearing a stringstream, impossible?

Started by peter_b Aug 16, 2006 at 3:27 AM 6 replies 1.5k views
Original Post
peter_b
peter_b
Sometimes i use stringstreams for various things. However, i have never been able to reuse a stringstream for several operations. This is because iv never been able to "clear" the old junk out of the stream. stringstream::clear() simply does just not work! It seems it does nothing. Anyone know what im doing wrong? std::stringstream ss; int value = 359; ss<<value; std::string str; str = ss.str(); // this far, all is good, str = "359" // now check this out: ss.clear() // should clear ss, no? it does not. int value2 = 111; ss<<value2; std::string str2; str2 = ss.str(); // now str2 contains "359111", while it should contain just "111" if ".clear()" would actually "clear" the contents of ss.
Shields up! Rrrrred alert!
bubu LV
bubu LV
Maybe read in manual, what actually clear() method does: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang98/HTML/IOS_basic_iosCCclear.asp#basic_ios::clear
Quote:
void clear(iostate state = goodbit);

The member function replaces the stored stream state information with state | (rdbuf() != 0 ? goodbit : badbit).


It has nothing to do with clearing stream contents.
Basiror
Basiror
stringstream ss;

ss.str("");

this will replace the string stored internally with a empty string "".
http://www.8ung.at/basiror/theironcross.html
Basiror
Basiror
Quote:
Original post by bubu LV
Maybe read in manual, what actually clear() method does: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang98/HTML/IOS_basic_iosCCclear.asp#basic_ios::clear
Quote:
void clear(iostate state = goodbit);

The member function replaces the stored stream state information with state | (rdbuf() != 0 ? goodbit : badbit).


It has nothing to do with clearing stream contents.


thr clear() function clears the flags only
http://www.8ung.at/basiror/theironcross.html
peter_b
peter_b
ah ok, i tought clear() was the same for all std containers.



however Basiror.
If i do this:

std::stringstream ss;
ss.str("");
int id = 359;
ss<std::string value = ss.str();

// then value will contain only "", not "359" =(
// any idea? seems setting the internal string prevents further writing via the << >> operators
Shields up! Rrrrred alert!
Brother Bob
Brother Bob
Quote:
Original post by peter_b
ah ok, i tought clear() was the same for all std containers.

stringstream is not a container.

Quote:
Original post by peter_b
however Basiror.
If i do this:

std::stringstream ss;
ss.str("");
int id = 359;
ss<std::string value = ss.str();

// then value will contain only "", not "359" =(
// any idea? seems setting the internal string prevents further writing via the << >> operators

Works as expected for me.
peter_b
peter_b
Quote:
Original post by Brother Bob
Quote:
Original post by peter_b
ah ok, i tought clear() was the same for all std containers.

stringstream is not a container.

Quote:
Original post by peter_b
however Basiror.
If i do this:

std::stringstream ss;
ss.str("");
int id = 359;
ss<std::string value = ss.str();

// then value will contain only "", not "359" =(
// any idea? seems setting the internal string prevents further writing via the << >> operators

Works as expected for me.




Ok it does, posted a simplified sample, my bad. But try this:

std::string value = "359";
std::stringstream ss;
ss<unsigned int id;
ss>>id;
id = id+1;
ss.str("");
ss<value = ss.str();

// value should contain "360", but it dosen't. Atleast not for me =/
Shields up! Rrrrred alert!
Fruny
Fruny
That's because you also need to clear the end-of-file flag from the stream. Thus, you need both ss.clear(); and ss.str("");.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.