By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,421 Members | 1,127 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,421 IT Pros & Developers. It's quick & easy.

How to reuse a ostrstream?

P: n/a
How do I reuse a ostrstream?

So far I have replaced all code that does this

"delete <ostrstream>.str()"

with

"<ostrstream>.freeze(0)"

to allow the <ostrstream>.dtor to perform it's house keeping.

Now I need to reuse the ostrstream in a few places and am starting to do
this instead of deleting and recreating a ostrstream

<ostrstream>.freeze(0);
<ostrstream>.seekp((strlen(<ostrstream>.str()) + 1), ios::cur);

Is this the correct way?

I have seen this in other code when browsing google but am unsure of
exactly what it is doing

<ostrstream>.seekp(0, ios::beg);

TIA.
Jul 22 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
"Charles Prince" <s_*************@yahoo.co.uk> wrote...
How do I reuse a ostrstream?
I don't know how _you_ reuse it.

Besides, why live in the past? It's been at least six years since
*strstreams were deprecated in favour of *stringstream. Why not
simply switch to using *stringstream?
[...]

Jul 22 '05 #2

P: n/a
"Victor Bazarov" <v.********@comAcast.net> wrote in message news:v2rpc.53219
"Charles Prince" <s_*************@yahoo.co.uk> wrote...
How do I reuse a ostrstream?


Does o.setp(0) work? To be safe, call o.clear() first to clear the failbit
and bitbit, in case they happen to be set.

o.clear();
o.setp(0);

Upon writing a new string I think you have to insert the null char. In the
days I used ostrstream I called o << ends; but I think o << char(0) should
work too, though the former looks clearer.

I don't know how _you_ reuse it.

Besides, why live in the past? It's been at least six years since
*strstreams were deprecated in favour of *stringstream. Why not
simply switch to using *stringstream?


Fine, though if we we're forced to maintain legacy code or use old compilers
and libraries, it might not be possible to make the switch.

Anyway, the question remains. How to clear the string of an ostringstream?
I guess you could use the method above for ostrstream (call clear and setp
and ends) or just call o.str(""). The latter method is clearer, but it
might reset the reserve capacity of the string to zero. Any thoughts?

Charles, to get ostringstream include <sstream>.
Jul 22 '05 #3

P: n/a
"Siemel Naran" <Si*********@REMOVE.att.net> wrote...
"Victor Bazarov" <v.********@comAcast.net> wrote in message news:v2rpc.53219
"Charles Prince" <s_*************@yahoo.co.uk> wrote...

How do I reuse a ostrstream?


Does o.setp(0) work? To be safe, call o.clear() first to clear the

failbit and bitbit, in case they happen to be set.

o.clear();
o.setp(0);

Upon writing a new string I think you have to insert the null char. In the days I used ostrstream I called o << ends; but I think o << char(0) should
work too, though the former looks clearer.

I don't know how _you_ reuse it.

Besides, why live in the past? It's been at least six years since
*strstreams were deprecated in favour of *stringstream. Why not
simply switch to using *stringstream?
Fine, though if we we're forced to maintain legacy code or use old

compilers and libraries, it might not be possible to make the switch.

Anyway, the question remains. How to clear the string of an ostringstream? I guess you could use the method above for ostrstream (call clear and setp
and ends) or just call o.str(""). The latter method is clearer, but it
might reset the reserve capacity of the string to zero. Any thoughts?


You surprise me. The topic of clearing an ostringstream has come up several
times over the past couple of years. And the answer is always the same,
use "o.str(string()),o.clear()" (the latter operation is needed in case your
ostringstream has been somehow violated).

Mind you, not all library implementations correctly handle clearing of
ostringstreams. However, that's an implementation issue.

Victor
Jul 22 '05 #4

P: n/a
"Victor Bazarov" <v.********@comAcast.net> wrote in message news:yfwpc.56104
Anyway, the question remains. How to clear the string of an ostringstream?
I guess you could use the method above for ostrstream (call clear and setp and ends) or just call o.str(""). The latter method is clearer, but it
might reset the reserve capacity of the string to zero. Any thoughts?


You surprise me. The topic of clearing an ostringstream has come up

several times over the past couple of years. And the answer is always the same,
use "o.str(string()),o.clear()" (the latter operation is needed in case your ostringstream has been somehow violated).


I wasn't reading this newsgroup for most of last year and the year before.
As for o.str(string()) it is the same as o.str(""). However, in most
implementations that would set the reserve capacity to zero, right?
Jul 22 '05 #5

P: n/a
"Siemel Naran" <Si*********@REMOVE.att.net> wrote...
"Victor Bazarov" <v.********@comAcast.net> wrote in message news:yfwpc.56104
Anyway, the question remains. How to clear the string of an ostringstream?
I guess you could use the method above for ostrstream (call clear and setp and ends) or just call o.str(""). The latter method is clearer, but it might reset the reserve capacity of the string to zero. Any thoughts?


You surprise me. The topic of clearing an ostringstream has come up

several
times over the past couple of years. And the answer is always the same,
use "o.str(string()),o.clear()" (the latter operation is needed in case

your
ostringstream has been somehow violated).


I wasn't reading this newsgroup for most of last year and the year before.


That's what the archives are for :-)
As for o.str(string()) it is the same as o.str(""). However, in most
implementations that would set the reserve capacity to zero, right?


It probably does. The Standard says that .str(string) deallocates the
buffer's underlying character sequence, and copies the string's one
there. Given an empty string, it will copy nothing. So, the buffer
is simply deallocated. Is that somehow a bid deal (aside from needing
some allocations in case of future outputs)?

Victor
Jul 22 '05 #6

P: n/a
On Sat, 15 May 2004 15:54:03 +0000, Victor Bazarov wrote:
"Charles Prince" <s_*************@yahoo.co.uk> wrote...
How do I reuse a ostrstream?


I don't know how _you_ reuse it.

Besides, why live in the past? It's been at least six years since
*strstreams were deprecated in favour of *stringstream. Why not
simply switch to using *stringstream?
[...]


Old compiler and code and I'm a bit too scared to fix what is not broken.
Jul 22 '05 #7

P: n/a
On Sat, 15 May 2004 20:56:47 +0000, Siemel Naran wrote:
"Victor Bazarov" <v.********@comAcast.net> wrote in message news:v2rpc.53219 <snip> Anyway, the question remains. How to clear the string of an ostringstream?
I guess you could use the method above for ostrstream (call clear and setp
and ends) or just call o.str(""). The latter method is clearer, but it
might reset the reserve capacity of the string to zero. Any thoughts?

Tried o.str("") but got adverse effects so I believe you are right here.
Charles, to get ostringstream include <sstream>.


Yeah thanks for the info.
Jul 22 '05 #8

P: n/a
On Fri, 14 May 2004 10:22:03 +0100, "Charles Prince"
<s_*************@yahoo.co.uk> wrote:
How do I reuse a ostrstream?

So far I have replaced all code that does this

"delete <ostrstream>.str()"
Was that delete[]? I hope so!

with

"<ostrstream>.freeze(0)"

to allow the <ostrstream>.dtor to perform it's house keeping.
That's sensible.
Now I need to reuse the ostrstream in a few places and am starting to do
this instead of deleting and recreating a ostrstream

<ostrstream>.freeze(0);
<ostrstream>.seekp((strlen(<ostrstream>.str()) + 1), ios::cur);

Is this the correct way?
No, it doesn't look like it. That is seeking way past the end of the
stream. I think you want:

<ostrstream>.clear(); //clear error state
<ostrstream>.freeze(false); //return memory control to stream
//make sure nothing is holding onto the return of str().
<ostrstream>.seekp(0, ios_base::beg); //seek to start of stream

You need to be careful about manipulators, etc. And remember to null
terminate output with std::ends, or be explicit about how many
characters you copy from str().
I have seen this in other code when browsing google but am unsure of
exactly what it is doing

<ostrstream>.seekp(0, ios::beg);


That's seeking to the start of the stream. This allows you to then
overwrite what used to be in the stream, thus "reusing" it.

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #9

P: n/a
"Nobody" <No****@nowhere.com> wrote in message
Anyway, the question remains. How to clear the string of an ostringstream? I guess you could use the method above for ostrstream (call clear and setp and ends) or just call o.str(""). The latter method is clearer, but it
might reset the reserve capacity of the string to zero. Any thoughts?


Tried o.str("") but got adverse effects so I believe you are right here.


What side effects?
Jul 22 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.