C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

clearing a stringstream

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
William Payne
Guest





PostPosted: Tue Sep 28, 2004 8:13 am    Post subject: clearing a stringstream Reply with quote



How do you get rid the contents in a std::stringstream? I'm using one in a
for loop to format some status messages which I print to the screen. The
stringstream member function clear() only clears flags (bits) in the stream,
right? I solved it by declaring the stringstream inside the loop body even
though I have need of another stringstream just after the loop. So for each
iteration the constructor of the stringstream will be executed. Is this how
I should go about it? *looks at sprintf()*

/ WP


Back to top
John Harrison
Guest





PostPosted: Tue Sep 28, 2004 8:33 am    Post subject: Re: clearing a stringstream Reply with quote




"William Payne" <mikas493_no_spam (AT) student (DOT) liu.se> wrote

Quote:
How do you get rid the contents in a std::stringstream? I'm using one in a
for loop to format some status messages which I print to the screen. The
stringstream member function clear() only clears flags (bits) in the
stream,
right?

Right.

Quote:
I solved it by declaring the stringstream inside the loop body even
though I have need of another stringstream just after the loop. So for
each
iteration the constructor of the stringstream will be executed. Is this
how
I should go about it? *looks at sprintf()*

Do this instead

std::stringstream buf;
....
buf.str(std::string()); // set contents of string stream

john



Back to top
William Payne
Guest





PostPosted: Tue Sep 28, 2004 9:23 am    Post subject: Re: clearing a stringstream Reply with quote




"John Harrison" <john_andronicus (AT) hotmail (DOT) com> wrote

Quote:

"William Payne" <mikas493_no_spam (AT) student (DOT) liu.se> wrote in message
news:cjb6di$pij$1 (AT) news (DOT) island.liu.se...
How do you get rid the contents in a std::stringstream? I'm using one in
a
for loop to format some status messages which I print to the screen. The
stringstream member function clear() only clears flags (bits) in the
stream,
right?

Right.

I solved it by declaring the stringstream inside the loop body even
though I have need of another stringstream just after the loop. So for
each
iteration the constructor of the stringstream will be executed. Is this
how
I should go about it? *looks at sprintf()*

Do this instead

std::stringstream buf;
...
buf.str(std::string()); // set contents of string stream

john



Thanks John, I will do that instead.

/ WP



Back to top
Shiju Rajan
Guest





PostPosted: Tue Sep 28, 2004 10:00 am    Post subject: Re: clearing a stringstream Reply with quote


Doesn't flush work for clearing the contents ........stringstream derives
from ostream and this class has a flush function to clear the buffer.
"William Payne" <mikas493_no_spam (AT) student (DOT) liu.se> wrote:
Quote:

"John Harrison" <john_andronicus (AT) hotmail (DOT) com> wrote in message
news:2rslu2F1d1co9U1 (AT) uni-berlin (DOT) de...

"William Payne" <mikas493_no_spam (AT) student (DOT) liu.se> wrote in message
news:cjb6di$pij$1 (AT) news (DOT) island.liu.se...
How do you get rid the contents in a std::stringstream? I'm using one
in
a
for loop to format some status messages which I print to the screen.
The
stringstream member function clear() only clears flags (bits) in the
stream,
right?

Right.

I solved it by declaring the stringstream inside the loop body even
though I have need of another stringstream just after the loop. So for
each
iteration the constructor of the stringstream will be executed. Is this
how
I should go about it? *looks at sprintf()*

Do this instead

std::stringstream buf;
...
buf.str(std::string()); // set contents of string stream

john



Thanks John, I will do that instead.

/ WP




Back to top
William Payne
Guest





PostPosted: Tue Sep 28, 2004 10:06 am    Post subject: Re: clearing a stringstream Reply with quote


"Shiju Rajan" <rajanrsh (AT) india (DOT) hp.com> wrote

Quote:

Doesn't flush work for clearing the contents ........stringstream derives
from ostream and this class has a flush function to clear the buffer.

No, everything is still there after a call to flush().

/ WP

Quote:
"William Payne" <mikas493_no_spam (AT) student (DOT) liu.se> wrote:

"John Harrison" <john_andronicus (AT) hotmail (DOT) com> wrote in message
news:2rslu2F1d1co9U1 (AT) uni-berlin (DOT) de...

"William Payne" <mikas493_no_spam (AT) student (DOT) liu.se> wrote in message
news:cjb6di$pij$1 (AT) news (DOT) island.liu.se...
How do you get rid the contents in a std::stringstream? I'm using one
in
a
for loop to format some status messages which I print to the screen.
The
stringstream member function clear() only clears flags (bits) in the
stream,
right?

Right.

I solved it by declaring the stringstream inside the loop body even
though I have need of another stringstream just after the loop. So for
each
iteration the constructor of the stringstream will be executed. Is this
how
I should go about it? *looks at sprintf()*

Do this instead

std::stringstream buf;
...
buf.str(std::string()); // set contents of string stream

john



Thanks John, I will do that instead.

/ WP






Back to top
John Harrison
Guest





PostPosted: Tue Sep 28, 2004 10:25 am    Post subject: Re: clearing a stringstream Reply with quote


"Shiju Rajan" <rajanrsh (AT) india (DOT) hp.com> wrote

Quote:

Doesn't flush work for clearing the contents ........stringstream derives
from ostream and this class has a flush function to clear the buffer.

No it doesn't. Flushing the buffer means committing the contents of the
buffer to the final destination. In the case of fstream this means writing
the buffer to a file. But in the case of stringstream the final destination
is the string object held internally in the stringstream object. So flushing
the buffer (if it did anything) would mean writing to the string held
internally in the stringstream object. Its that string that William is
trying to empty, not write to.

john



Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.