473,795 Members | 2,892 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to reuse a ostrstream?

How do I reuse a ostrstream?

So far I have replaced all code that does this

"delete <ostrstream>.st r()"

with

"<ostrstream>.f reeze(0)"

to allow the <ostrstream>.dt or 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>.fr eeze(0);
<ostrstream>.se ekp((strlen(<os trstream>.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>.se ekp(0, ios::beg);

TIA.
Jul 22 '05 #1
9 7465
"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.********@com Acast.net> wrote in message news:v2rpc.5321 9
"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*********@RE MOVE.att.net> wrote...
"Victor Bazarov" <v.********@com Acast.net> wrote in message news:v2rpc.5321 9
"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.********@com Acast.net> wrote in message news:yfwpc.5610 4
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*********@RE MOVE.att.net> wrote...
"Victor Bazarov" <v.********@com Acast.net> wrote in message news:yfwpc.5610 4
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.********@com Acast.net> wrote in message news:v2rpc.5321 9 <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>.st r()"
Was that delete[]? I hope so!

with

"<ostrstream>. freeze(0)"

to allow the <ostrstream>.dt or 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>.f reeze(0);
<ostrstream>.s eekp((strlen(<o strstream>.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>.cl ear(); //clear error state
<ostrstream>.fr eeze(false); //return memory control to stream
//make sure nothing is holding onto the return of str().
<ostrstream>.se ekp(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>.s eekp(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
1547
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
3871
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 things like #include <strstream.h> or
3
5610
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
1636
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
2804
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
1835
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
4982
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 related to 'ostrstream' and the fact that it is being deprecated from the language, etc. What is the best way to handle fixing this replicating error? TIA, Eric
1
2381
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 writes error messages to this stream using the “<<” operator. 4) The EXE file creates an ostrstream variable and passes this to the DLL function. 5) After the DLL function has finished, the EXE writes some text to an output file, followed by the...
19
2386
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 in the Sky,' Vernor Vinge described a future where software is created by 'programmer archaeologists' who search archives for existing pieces of code, contextualize them, and combine them into new applications. So
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9522
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10443
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9044
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6783
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5565
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2921
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.