Article View: gnu.g++.help
Article #839Re: initialization of ostrstream
From: Enno Sandner
Date: Tue, 28 Nov 1995 08:12
Date: Tue, 28 Nov 1995 08:12
37 lines
1747 bytes
1747 bytes
In article <199511272135.WAA02816@rulcmc.LeidenUniv.nl> 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?
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've missed to terminate the string. Use 'ends' for an additional
0-char at the end of the string, ie. exchange 'os<<"hello, world";'
by 'os<<"hello, world"<<ends;'.
regards
Enno
Technical Details
Message-ID:
<ENNO.95Nov28091208@kitz.inferenzsysteme.informatik.th-darmstadt.de>Path:
arc1-ca.pugleaf.net!news.th-darmstadt.de!news!ennoReferences:
<199511272135.WAA02816@rulcmc.LeidenUniv.nl>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!news.th-darmstadt.de!news!enno From: enno@inferenzsysteme.informatik.th-darmstadt.de (Enno Sandner) Newsgroups: gnu.g++.help,comp.lang.c++ Subject: Re: initialization of ostrstream Date: 28 Nov 1995 08:12:05 GMT Organization: Fachbereich Informatik, TH Darmstadt Distribution: world Message-ID: <ENNO.95Nov28091208@kitz.inferenzsysteme.informatik.th-darmstadt.de> References: <199511272135.WAA02816@rulcmc.LeidenUniv.nl> In-reply-to: joost@rulcmc.leidenuniv.nl's message of 27 Nov 1995 16:37:47 -0500