Article View: gnu.g++.help
Article #844Re: initialization of ostrstream
From: Joe Buck
Date: Wed, 29 Nov 1995 00:50
Date: Wed, 29 Nov 1995 00:50
39 lines
1678 bytes
1678 bytes
joost@rulcmc.leidenuniv.nl (joost witteveen) writes:
>If I construct a ostrstream, with
> ostrstream os (s,sizeof(s))
>do I have to clean ALL of s before I use it?
No.
>I'm getting strange results (i.e. I have to init ALL of s to 0)
>with g++ 2.7.1, and 2.3.3.
You're getting the expected results.
>So, my question is: is this a bug in g++, or am I missing something
>about the string streams?
You're missing the fact that you have to do the null termination yourself.
There is a special function-object named ends that does this for you.
You may also be assuming that the null character in the s string does
something. It does not. ostrstream does not use the strcat mechanism
to append. It keeps track of the ending position and writes there.
Here's your fixed program:
#include <iostream.h>
#include <strstream.h>
int main(int,char **){
int i;
char s[]="\000hhhhhhhhhhhhhhhhhhhhhhhhhhhh";
cout<<"initial s:"<<s<<endl; //an empty string here.
ostrstream os(s,sizeof(s));
os<<"hello, world" << ends; // ends terminates the string
cout<<s<<endl;
}
--
-- Joe Buck <jbuck@synopsys.com> (not speaking for Synopsys, Inc)
Anagrams for "information superhighway": Enormous hairy pig with fan
A rough whimper of insanity
Technical Details
Message-ID:
<49gao7$guu@hermes.synopsys.com>Path:
arc1-ca.pugleaf.net!news.synopsys.com!jbuckReferences:
<199511272135.WAA02816@rulcmc.leidenuniv.nl>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!news.synopsys.com!jbuck From: jbuck@synopsys.com (Joe Buck) Newsgroups: gnu.g++.help,comp.lang.c++ Subject: Re: initialization of ostrstream Date: 29 Nov 1995 00:50:14 GMT Organization: Synopsys Inc., Mountain View, CA 94043-4033 Message-ID: <49gao7$guu@hermes.synopsys.com> References: <199511272135.WAA02816@rulcmc.leidenuniv.nl> NNTP-Posting-Host: deerslayer.synopsys.com