🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: alt.comp.lang.learn.c-c++
Article #756

Re: Cannot reset an ostrstream back to start position

#756
From: John Harrison
Date: Sat, 07 Apr 2001 14:33
65 lines
2842 bytes
"Nicolas Reimen" <Nicolas.reimen@acm.org> wrote in message
news:9amrrc$1v3r$1@news5.isdnet.net...
> This code :
>
> #include <string>
> #include <iostream>
> #include <strstream>
>
> using namespace std;
>
> int main(int argc, char* argv[])
> {
>  ostrstream ss;
>
>  ss << "foo "<< 1234 << ends;
>  printf("%s\n", ss.str());
>  printf("tellp %d\n", ss.tellp());
>  ss.seekp(0);
>  printf("tellp %d\n", ss.tellp());
>  ss << "bar "<< 5678 << ends;
>  printf("%s\n", ss.str());
>
>  return 0;
> }
>
> should ( from my naive point of view ) produce the following output:
>
> foo 1234
> tellp 9
> tellp 0
> bar 5678
>
> But, what I get is :
>
> foo 1234
> tellp 9
> tellp 0
> foo 1234
>
> The insert pointer is correctly set back to 0, it seems, but
> the ostrtream still does not want to overwrite what is
> already in the buffer.
>
> Can anyone help ?
>
> PS: I am using MS VC++6 and I found the advice to
> use seekp in msdn, but without more details or
> an example :-(
>

You'd be better off switching to stringstream it's much easier to handle.
However your problem is that

ss.str()

freezes the strstream, preventing further modifcation. You need to add

ss.freeze(false);

after the call to ss.str(). Without this call you also have a memory leak so
you should add it after the second call to ss.str() as well (or delete the
memory by hand).

john
Technical Details
Message-ID: <EwFz6.4181$IP5.863960@news2-win.server.ntlworld.com>
Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!supernews.com!cyclone2.usenetserver.com!news-out.usenetserver.com!newsfeed.icl.net!newsfeed.icl.net!diablo.netcom.net.uk!netcom.net.uk!nntp.news.xara.net!xara.net!gxn.net!news5-gui.server.ntli.net!ntli.net!news2-win.server.ntlworld.com.POSTED!not-for-mail
References: <9amrrc$1v3r$1@news5.isdnet.net>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: fdcad,bbe7da431f112f2b
X-Google-Attributes: gidfdcad,public
X-Google-Thread: 114592,bbe7da431f112f2b
X-Google-Attributes: gid114592,public
X-Google-Thread: 109fba,bbe7da431f112f2b
X-Google-Attributes: gid109fba,public
X-Google-ArrivalTime: 2001-04-07 07:38:01 PST
Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!supernews.com!cyclone2.usenetserver.com!news-out.usenetserver.com!newsfeed.icl.net!newsfeed.icl.net!diablo.netcom.net.uk!netcom.net.uk!nntp.news.xara.net!xara.net!gxn.net!news5-gui.server.ntli.net!ntli.net!news2-win.server.ntlworld.com.POSTED!not-for-mail
Reply-To: "John Harrison" <jahhaj@bigfoot.com>
From: "John Harrison" <jahhaj@bigfoot.com>
Newsgroups: alt.comp.lang.learn.c-c++,comp.lang.c++,fr.comp.lang.c++
References: <9amrrc$1v3r$1@news5.isdnet.net>
Subject: Re: Cannot reset an ostrstream back to start position
Lines: 69
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.50.4522.1200
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4522.1200
Message-ID: <EwFz6.4181$IP5.863960@news2-win.server.ntlworld.com>
Date: Sat, 7 Apr 2001 15:33:07 +0100
NNTP-Posting-Host: 213.104.55.60
X-Complaints-To: abuse@ntlworld.com
X-Trace: news2-win.server.ntlworld.com 986653860 213.104.55.60 (Sat, 07 Apr 2001 15:31:00 BST)
NNTP-Posting-Date: Sat, 07 Apr 2001 15:31:00 BST
Organization: ntlworld News Service