473,396 Members | 1,875 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to reuse a ostrstream?

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
9 7433
"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
"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
"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
"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
"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
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: becte | last post by:
I encountered the following code similar to this // some header files static char* func(int i) { ostrstream out; if (i==1) out << "ABCDE"; else if (i==2) out << "123"; else cout << "";
2
by: b83503104 | last post by:
Hi, An old code is using stuff like #include <strstream.h> ostrstream *m_actionStream; and the compiler complained syntax error before `*' token I followed some previous posts and tried...
3
by: Mathieu Malaterre | last post by:
Hello, I am trying to write this simple code: std::ostringstream s; s << 1024; std::cout << s.str() << std::endl; s.str(""); // <- problem s << 512; std::cout << s.str() << std::endl;
2
by: Pep | last post by:
I have inherited a program that does this ostrstream os; os << "some text"; os.str() = '\0'; In fact is there any point doing it at all? TIA, Pep.
1
by: tcl | last post by:
Questions on ostrstream. #1) do I have a memory leak as the control exits the scope { ostrstream os; os << "hello world" << endl << ends; }
3
by: Simon | last post by:
Hi all, I'm hoping that some of you clever chaps could offer me some advice on code reuse. You see, whenever I make applications, I typically only find very limited
6
by: Eric Chomko | last post by:
I inherited a task which uses a lot of 'ostrstream' in the code. The port is from an SGI box to Sun which is using GNU C++ (g++ version 3.4.2). When trying to compile I get several errors all...
1
by: GeeBee | last post by:
1) I’m using Borland C++ Builder 1.0 (a very old (but still good) version). 2) I have an application EXE calling a DLL. 3) A function in the DLL receives a parameter defined as "ostream &" , and...
19
by: jacob navia | last post by:
There is an interesting discussion running in Slashdot now, about code reuse. The thema of the discussion is here: < quote > Susan Elliot Sim asks: "In the science fiction novel, 'A Deepness...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.