[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

sprintf equivalent in c++

A.Leopold

10/9/2008 1:34:00 PM

hello,

is there a c++ equivalent function to the c-function 'sprintf'?

Thank you,

leo

50 Answers

Pete Becker

10/9/2008 1:37:00 PM

0

On 2008-10-09 09:33:41 -0400, "A.Leopold" <andreas.leopold@himt.de> said:

>
> is there a c++ equivalent function to the c-function 'sprintf'?
>

Yes. Its name is sprintf.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

A.Leopold

10/9/2008 1:45:00 PM

0

;)
ok, I asked, cause my compiler (VS05) is complaining about the use of
sprintf ....

Pete Becker schrieb:
> On 2008-10-09 09:33:41 -0400, "A.Leopold" <andreas.leopold@himt.de> said:
>
>>
>> is there a c++ equivalent function to the c-function 'sprintf'?
>>
>
> Yes. Its name is sprintf.
>

juanvicfer

10/9/2008 2:04:00 PM

0

On Oct 9, 3:33 pm, "A.Leopold" <andreas.leop...@himt.de> wrote:
> hello,
>
> is there a c++ equivalent function to the c-function 'sprintf'?

Maybe you want to use stringstream.

>
> Thank you,
>
> leo

A.Leopold

10/9/2008 2:12:00 PM

0



juanvicfer schrieb:
> On Oct 9, 3:33 pm, "A.Leopold" <andreas.leop...@himt.de> wrote:
>> hello,
>>
>> is there a c++ equivalent function to the c-function 'sprintf'?
>
> Maybe you want to use stringstream.

ok, I will have a look at stringstream

sean_in_raleigh

10/9/2008 3:06:00 PM

0

On Oct 9, 9:33 am, "A.Leopold" <andreas.leop...@himt.de> wrote:
> hello,
>
> is there a c++ equivalent function to the c-function 'sprintf'?


You can also check out boost::format, which can be
easier to work with than stringstream:

http://www.boost.org/doc/libs/1_36_0/libs/format/doc/f...

Sean

A.Leopold

10/9/2008 3:13:00 PM

0



sean_in_raleigh@yahoo.com schrieb:
> On Oct 9, 9:33 am, "A.Leopold" <andreas.leop...@himt.de> wrote:
>> hello,
>>
>> is there a c++ equivalent function to the c-function 'sprintf'?
>
>
> You can also check out boost::format, which can be
> easier to work with than stringstream:
>
> http://www.boost.org/doc/libs/1_36_0/libs/format/doc/f...
>
> Sean

thank you, that looks much more convenient!

pjb

10/9/2008 3:32:00 PM

0

"A.Leopold" <andreas.leopold@himt.de> writes:

> ;)
> ok, I asked, cause my compiler (VS05) is complaining about the use of
> sprintf ....

I guess it's rather ::sprintf (with extern "C" { / #include <stdio.h> / }).

But if you want some safety, you'd use ::snprintf, and if you don't want to implement
the safety yourself, you could rather use lisp^W std::ostringstream.

std::ostringstream s; s<<"Hi "<<world<<std::endl;
std::string str =s.str();
const char* cstr=s.c_str();

--
__Pascal Bourguignon__

jt

10/9/2008 4:15:00 PM

0

On Thu, 09 Oct 2008 17:12:56 +0200, A.Leopold wrote:

> sean_in_raleigh@yahoo.com schrieb:
>> On Oct 9, 9:33 am, "A.Leopold" <andreas.leop...@himt.de> wrote:
>>> hello,
>>>
>>> is there a c++ equivalent function to the c-function 'sprintf'?
>>
>>
>> You can also check out boost::format, which can be easier to work with
>> than stringstream:
>>
>> http://www.boost.org/doc/libs/1_36_0/libs/format/doc/f...
>>
>> Sean
>
> thank you, that looks much more convenient!

There is also boost::lexical_cast (something like a "cheap and cheerful"
version of stringstream formatting):

http://www.boost.org/doc/libs/1_36_0/libs/conversion/lexica...

--
Lionel B

Default User

10/9/2008 5:59:00 PM

0

Pascal J. Bourguignon wrote:

> "A.Leopold" <andreas.leopold@himt.de> writes:
>
> > ;)
> > ok, I asked, cause my compiler (VS05) is complaining about the use
> > of sprintf ....
>
> I guess it's rather ::sprintf (with extern "C" { / #include <stdio.h>
> / }).

What would make you guess something like that?




Brian

pjb

10/10/2008 11:48:00 AM

0

"Default User" <defaultuserbr@yahoo.com> writes:

> Pascal J. Bourguignon wrote:
>
>> "A.Leopold" <andreas.leopold@himt.de> writes:
>>
>> > ;)
>> > ok, I asked, cause my compiler (VS05) is complaining about the use
>> > of sprintf ....
>>
>> I guess it's rather ::sprintf (with extern "C" { / #include <stdio.h>
>> / }).
>
> What would make you guess something like that?

First, AFAIK, C functions are in the "root" namespace; to avoid
refering a different object in the current namespace, it's advised (in
various style guides) to qualify C functions.

Then, using a function without declaring it would make a sane compiler
complain, so it's good practice to include some header declaring it
before using it.


--
__Pascal Bourguignon__