Topic: HELP! need C++ equvalent of sprintf
Author: rsedlak@booz.bah.com (Richard Sedlak)
Date: 25 Jan 1994 13:50:01 -0500 Raw View
adaga@asic.mtv.nec.com wrote:
: Is there any similar C++ command to C's "sprintf"?
: I have a mixture of data types (ie. int, floats, strings, etc.) that I want
: to put into one string, in their proper formats. If I use sprintf, I have
: to include "stdio.h", and since this is the only function that needs this
: include file, I'd like to replace it with a similar C++ command.
Why not just use sprintf() ? Sure adding <stdio.h> will increase the size
but the costs shouldn't be that high.
--
Richard P. Sedlak Jr
Booz*Allen & Hamilton, Inc.
rsedlak@bah.com
(800) 862-4511 ext. 9515
Author: bannerma@bmerhb19.bnr.ca (Steve Bannerman)
Date: 25 Jan 1994 21:20:43 GMT Raw View
I'm not sure if it's right or not but you can create an [io]strstream instance
(using the char location you want) and then use the << or >> operators to
input or output your ints, floats, etc... to your stream. Comments on
correctness??
Author: anders@munin.his.se (Anders Eklund)
Date: 26 Jan 94 15:19:19 GMT Raw View
In article <2i42fb$mvc@bmerha64.bnr.ca> bannerma@bmerhb19.bnr.ca (Steve Bannerman) writes:
Path: his!news.chalmers.se!sunic!EU.net!uknet!bnr.co.uk!bnrgate!bmerha64.bnr.ca!bmerhb19!bannerma
From: bannerma@bmerhb19.bnr.ca (Steve Bannerman)
Newsgroups: comp.std.c++
Date: 25 Jan 94 21:20:43 GMT
References: <1994Jan21.180038.258@asic.mtv.nec.com> <2i3pkp$nno@booz.bah.com>
> Sender: bannerma@bmerhb19 (Steve Bannerman)
> Organization: Bell-Northern Research Ltd, Ottawa, Canada
> Lines: 4
> NNTP-Posting-Host: bmerhb19.bnr.ca
>
> I'm not sure if it's right or not but you can create an [io]strstream instance
> (using the char location you want) and then use the << or >> operators to
> input or output your ints, floats, etc... to your stream. Comments on
> correctness??
>
That is correct. It is quite possible to use strstream. First write to
the stream with << operator using whatever datatype You want. Then
read the output back into a string. If several values were written
to the stream with spaces between the values it is not appropiate
to read it back into the string using >> because a space works as
a delimiter and only the first value will be read. It is better
to use the getline operation which reads a maximum number of chars
or until a specified delimiter is read (default \n).
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
___
/ / /
/__ /___ ___/___ ___ ___ -- Anders Eklund anders@his.se --
/ // // //__// /__ -- Computer Science Department --
_/ // //__//__ / ___/ -- University of Skovde, Sweden --
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Author: adaga@asic.mtv.nec.com
Date: 21 Jan 94 18:00:38 -0800 Raw View
Is there any similar C++ command to C's "sprintf"?
I have a mixture of data types (ie. int, floats, strings, etc.) that I want
to put into one string, in their proper formats. If I use sprintf, I have
to include "stdio.h", and since this is the only function that needs this
include file, I'd like to replace it with a similar C++ command.
Any suggestions would greatly be appreciated.
Anthony Daga
anthony@asic.mtv.nec.com