Article View: gnu.g++.help
Article #842Re: initialization of ostrstream
From: Bernd Eggink
Date: Tue, 28 Nov 1995 13:18
Date: Tue, 28 Nov 1995 13:18
46 lines
1931 bytes
1931 bytes
joost witteveen (joost@rulcmc.leidenuniv.nl) wrote:
> If I construct a ostrstream, with
> ostrstream os (s,sizeof(s))
> do I have to clean ALL of s before I use it?
> 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.
> So, my question is: is this a bug in g++, or am I missing something
> about the string streams?
> The following programme has this what I think is strange behaviour,
> as it outputs in the last line not only the "hello, world", but
> part of the old h's string s was inititalized to, concatenated to it.
> Thanks for any suggestions.
> #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";
> cout<<s<<endl; //outputs: hello, worldhhhhhhhhhhhhhhhhh
> }
You forgot to append a '\0', which is _not_ done automatically by
'ostrstream'. There is a special manipulator 'ends' which does exactly
this:
os << "hello world" << ends;
Now the string is correctly '\0'-terminated.
Bernd
--
+----------------------------------+
| Bernd Eggink |
| Rechenzentrum Uni Hamburg |
| admin@rzaix13.rrz.uni-hamburg.de |
+----------------------------------+
Technical Details
Message-ID:
<49f273$73k@rzsun02.rrz.uni-hamburg.de>Path:
arc1-ca.pugleaf.net!news.rrz.uni-hamburg.de!rzaix13!adminReferences:
<199511272135.WAA02816@rulcmc.LeidenUniv.nl>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!news.rrz.uni-hamburg.de!rzaix13!admin From: admin@rzaix13.uni-hamburg.de (Bernd Eggink) Newsgroups: gnu.g++.help,comp.lang.c++ Subject: Re: initialization of ostrstream Followup-To: gnu.g++.help,comp.lang.c++ Date: 28 Nov 1995 13:18:27 GMT Organization: University of Hamburg -- Germany Distribution: world Message-ID: <49f273$73k@rzsun02.rrz.uni-hamburg.de> References: <199511272135.WAA02816@rulcmc.LeidenUniv.nl> NNTP-Posting-Host: rzaix13.rrz.uni-hamburg.de X-Newsreader: TIN [version 1.2 PL2]