🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.g++.help
Article #753

Re: ostrstream

#753
From: J. Kanze
Date: Tue, 29 Aug 1995 16:08
55 lines
2338 bytes
Peter Berdeklis (peter@chinook.physics.utoronto.ca) wrote:
|> According to Albert Golembiowski <albert@inrs-telecom.uquebec.ca>:
|> > 
|> > I found that `ostrstream buf' object must be explicitely
|> > terminated by '\0' to hand off a char string properly.
|> > 
|> >  [Q1] Is this standard, or a bug?

|> Standard.

Sort of.  There is no ostrstream in the standard, but the generally
accepted behavior is as described.

|> >  [Q2] Couldn't the method `buf.str()' do it for me?

|> How would it figure out where you wanted to end the string?

How about at the end of the string?  It could.  Maybe it should have.
It doesn't.

In practice, there would be one problem with letting str() do the job:
the insertion of the '\0' can fail.  But of course, you obviously want
to check and make sure that it succeeded before using the pointer.

|> > I don't see much use for a char string picking up
|> > tail-end "star dust".

|> Consider this snippet:

|> {
|> ostream buf;

|> buf << "Start the string...";
|> do_some_cool_stuff();
|> buf << "Continue the string...";
|> do_some_boring_stuff();
|> buf << "End the string." << ends;
|> }


|> Consider the extra housekeeping required if << added a NULL at the
|> end of each insertion.  Very slow.  Better to ask the smart programmer.
|> ;-)

1. Because of the way ostream interacts with streambuf, you would in
fact have to add a '\0' after each character.  Still, I don't think it
would be that slow.

2. The other alternative is just to have the str function add the '\0'.
This is simple: you only add it when you need it.
-- 
James Kanze           (+33) 88 14 49 00          email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
Conseils en informatique industrielle--
                             --Beratung in industrieller Datenverarbeitung
Technical Details
Message-ID: <41ve2f$2hk@gabi.gabi-soft.fr>
Path: arc1-ca.pugleaf.net!gabi-soft.fr!not-for-mail
References: <9508281944.AA10055@vital> <DE1xy6.EyI@info.physics.utoronto.ca>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!gabi-soft.fr!not-for-mail
From: kanze@gabi-soft.fr (J. Kanze)
Newsgroups: gnu.g++.help,comp.lang.c++
Subject: Re: ostrstream
Followup-To: gnu.g++.help,comp.lang.c++
Date: 29 Aug 1995 18:08:47 +0200
Organization: GABI Software, Sarl.
Message-ID: <41ve2f$2hk@gabi.gabi-soft.fr>
References: <9508281944.AA10055@vital> <DE1xy6.EyI@info.physics.utoronto.ca>
NNTP-Posting-Host: gabi.gabi-soft.fr
X-Newsreader: TIN [version 1.2 PL2]