clearing a stringstream

Started by
5 comments, last by ChaosEngine 21 years ago
is there anyway to clear the buffer of a stringstream? I''m using one to hold a description string that''s writen to in a serialise method, but I want to be able to change it after a certain point. any ideas? "That''s not a bug, it''s a feature!" --me
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight
Advertisement
nevermind figured it out.. doh!

std::stringstream stream;
stream.str("");

"That''s not a bug, it''s a feature!"
--me
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight
I think clear or flush would have worked too.
quote:Original post by ph33r
I think clear or flush would have worked too.


stringstream::clear() doesn''t clear the buffer, but the stream flags (eof, fail, bad). As such, it is required too.


std::stringstream stream;
stream.clear();
stream.str("");



[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"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
ph33r, already tried those and they didn't work.

thanks, fruny. forgot about the flags!

"That's not a bug, it's a feature!"
--me

[edited by - ChaosEngine on April 1, 2003 2:29:22 AM]
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight
edit: just ignore me - see fruny below

what about...?

std::stringstream stream;
... do stuff
stream = std::stringstream();

[edited by - petewood on April 1, 2003 3:24:13 AM]
Standard iostreams are not assignable. If they were, your method would reset all the stream''s state (format option, locale, user data...) not just clear the buffer.

Swapping the rdbuf() with a local one isn''t a very good idea either (lifetime issue), nor is dynamic allocation (ha!)

[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"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

This topic is closed to new replies.

Advertisement