How to reuse a ostrstream?

C

Charles Prince

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.
 
V

Victor Bazarov

Charles Prince said:
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?
 
S

Siemel Naran

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>.
 
V

Victor Bazarov

Siemel Naran said:
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.



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
 
S

Siemel Naran

Victor Bazarov said:
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?
 
V

Victor Bazarov

Siemel Naran said:
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
 
N

Nobody

Charles Prince said:
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.
 
N

Nobody

"Victor Bazarov" <[email protected]> wrote in message news:v2rpc.53219
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.
 
T

tom_usenet

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top