Thread View: gnu.g++.help
5 messages
5 total messages
Started by Albert Golembiowski
Mon, 28 Aug 1995 19:45
ostrstream
Author: Albert Golembiowski
Date: Mon, 28 Aug 1995 19:45
Date: Mon, 28 Aug 1995 19:45
34 lines
1494 bytes
1494 bytes
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?
[Q2] Couldn't the method `buf.str()' do it for me?
I don't see much use for a char string picking up
tail-end "star dust".
This snippet demonstrates my point:
--------------------- snip -------------------------
#include <strstream.h>
template <class Type>
Str& operator < (Str& Out, Vect<Type>& V) // insert
{//__~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ostrstream buf;
V.print(buf,"","",""); // put out vector values
buf << '\0'; // why is this necessary?
char *pb = buf.str();
Out = Str(pb);
delete pb;
return Out;
}
--------------------- snip -------------------------
Thank you,
--
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Albert Golembiowski albert@inrs-telecom.uquebec.ca (514) 762-8655
INRS Telecommunications, 16 Place du Commerce, Verdun, Quebec, Canada, H3E 1H6
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Re: ostrstream
Author: Peter Berdeklis
Date: Tue, 29 Aug 1995 03:06
Date: Tue, 29 Aug 1995 03:06
36 lines
1266 bytes
1266 bytes
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.
> [Q2] Couldn't the method `buf.str()' do it for me?
How would it figure out where you wanted to end the string?
>
> 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.
;-)
Pete
Re: ostrstream
Author: J. Kanze
Date: Tue, 29 Aug 1995 16:08
Date: Tue, 29 Aug 1995 16:08
55 lines
2338 bytes
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
Re: ostrstream
Author: Brandon Wallace
Date: Thu, 31 Aug 1995 19:14
Date: Thu, 31 Aug 1995 19:14
37 lines
1700 bytes
1700 bytes
On 28 Aug 1995, Albert Golembiowski wrote:
=> I don't see much use for a char string picking up
=> tail-end "star dust".
=> --------------------- snip -------------------------
=> #include <strstream.h>
=> template <class Type>
=> Str& operator < (Str& Out, Vect<Type>& V) // insert
=> {//__~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
=> ostrstream buf;
=> V.print(buf,"","",""); // put out vector values
=> buf << '\0'; // why is this necessary?
=> char *pb = buf.str();
=> Out = Str(pb);
=> delete pb;
=> return Out;
=> }
=> --------------------- snip -------------------------
Well, here is one example where you don't want the '\0' appended onto the end:
---- snip ----
voir example (void)
{
static char buf[100];
ostrstream ostr(buf, 100);
for (int i = 0; i < 100; i++) ostr << 'a';
char *str = ostr.str(); // Certainly don't want to append a '\0', since buf[] already has 100 chars in it!
}
--- snip ---
Brandon Wallace
Nicholas | Applegate Capital Management
mailto:bman@criterion.com http://www.criterion.com/home-pages/brandon
"I live life face down in the fast lane."
Re: ostrstream
Author: Brandon Wallace
Date: Fri, 01 Sep 1995 15:00
Date: Fri, 01 Sep 1995 15:00
34 lines
1962 bytes
1962 bytes
On 30 Aug 1995, Donald Hunter wrote:
=> In article <41ve2f$2hk@gabi.gabi-soft.fr> kanze@gabi-soft.fr (J. Kanze) writes:
=>
=> >
=> > 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.
=> >
=>
=> I think this behaviour would be desirable. str() returns a pointer to
=> a string and by convention these are terminated with a '\0'. It is
=> quite dangerous for str() to return a pointer to a character buffer
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
=> which is not terminated with a '\0'.
=>
=> Donald.
You answered your own question. str() returns a pointer to a character
buffer, *not* a pointer to a string!
If you are using strstream to fill in a CHARACTER BUFFER, then you
certainly don't want str() mucking the works by appending a '\0'.
In fact, in my manual, the description reads:
strstream - stream class for "I/O" using character arrays
It says nothing about nul-terminated strings.
I think it is perfectly reasonable to make you go: ostr << ends;
before calling str();
--
Brandon Wallace
Nicholas | Applegate Capital Management
mailto:bman@criterion.com http://www.criterion.com/home-pages/brandon
"I live life face down in the fast lane."
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads