| View previous topic :: View next topic |
| Author |
Message |
William Payne Guest
|
Posted: Tue Sep 28, 2004 8:13 am Post subject: clearing a stringstream |
|
|
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
|
Posted: Tue Sep 28, 2004 8:33 am Post subject: Re: clearing a stringstream |
|
|
"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
|
Posted: Tue Sep 28, 2004 9:23 am Post subject: Re: clearing a stringstream |
|
|
"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
|
Posted: Tue Sep 28, 2004 10:00 am Post subject: Re: clearing a stringstream |
|
|
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
|
Posted: Tue Sep 28, 2004 10:06 am Post subject: Re: clearing a stringstream |
|
|
"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
|
Posted: Tue Sep 28, 2004 10:25 am Post subject: Re: clearing a stringstream |
|
|
"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 |
|
 |
|