Connecting Tech Pros Worldwide Help | Site Map

printf or something like it

hswerdfe
Guest
 
Posts: n/a
#1: Aug 19 '05
is there a standard function that formats data the same way that printf
and sprintf do.

only the return value is a char*/string of the formated values.

I was wanting to do something like this

double myVar = rand();
MyFunc(NEWprintf("val=%f", myVar));

where MyFunc takes some formated string like data.

and if there is not. what was the logic behind not making it?
Andre Kostur
Guest
 
Posts: n/a
#2: Aug 19 '05

re: printf or something like it


hswerdfe <hswerdfe@example.com> wrote in news:de4oeo$417$1@nrc-news.nrc.ca:
[color=blue]
> is there a standard function that formats data the same way that printf
> and sprintf do.[/color]

Yep. std::printf and std::sprintf.
[color=blue]
> only the return value is a char*/string of the formated values.[/color]

Not in Standard C++.
[color=blue]
> I was wanting to do something like this
>
> double myVar = rand();
> MyFunc(NEWprintf("val=%f", myVar));
>
> where MyFunc takes some formated string like data.
>
> and if there is not. what was the logic behind not making it?[/color]

Well.. if it were to return a char*, what would it point at?

As for returning a string... that's an easy wrapper to write. You may wish
to look up std::sprintf and stringstreams. Also, perhaps boost::format.
Victor Bazarov
Guest
 
Posts: n/a
#3: Aug 19 '05

re: printf or something like it


hswerdfe wrote:[color=blue]
> is there a standard function that formats data the same way that printf
> and sprintf do.[/color]

Why? Don't printf or sprintf work for you?
[color=blue]
> only the return value is a char*/string of the formated values.[/color]

No, such function doesn't exist, but you could write it yourself, all
the ingredients are there.
[color=blue]
> I was wanting to do something like this
>
> double myVar = rand();
> MyFunc(NEWprintf("val=%f", myVar));
>
> where MyFunc takes some formated string like data.
>
> and if there is not. what was the logic behind not making it?[/color]

The Standard Library is not meant to contain any function imaginable, only
every function _necessary_. Since you can make one from 'vsprintf' and
'va_arg' and 'va_list' macros, there is no need to bloat the library with
something rarely used.

V
pillbug
Guest
 
Posts: n/a
#4: Aug 19 '05

re: printf or something like it


hswerdfe wrote:[color=blue]
> is there a standard function that formats data the same way that printf
> and sprintf do.
>
> only the return value is a char*/string of the formated values.
>[/color]

#include <cstdarg>
char* mysprintf (char* buf, const char* fmt, ...)
{
va_list va;
va_start (va, fmt);
vsprintf (buf, fmt, va);
va_end (va);
return buf;
}
pillbug
Guest
 
Posts: n/a
#5: Aug 19 '05

re: printf or something like it


[color=blue]
> #include <cstdarg>
> char* mysprintf (char* buf, const char* fmt, ...)
> {
> va_list va;
> va_start (va, fmt);
> vsprintf (buf, fmt, va);
> va_end (va);
> return buf;
> }[/color]


i guess i should mention this is totally unsafe, not that
using sprintf is safe to begin with. at least sprintf gives
you the chance to detect an overrun via return value; using
it this way will get you in trouble.

hopefully, it shows you how to use the va_list macros,
which is what i think you really wanted to know.

ideally you would change the prototype to something like:

char* mysnprintf (char* buf, size_t nbuf, const char* fmt, ...)
{
va_list va;
va_start (va, fmt);
if (vsprintf (buf, fmt, va) > nbuf)
throw std::overflow_error ("mysnprintf");
va_end (va);
return buf;
}
Pete Becker
Guest
 
Posts: n/a
#6: Aug 19 '05

re: printf or something like it


pillbug wrote:[color=blue]
>
>
> i guess i should mention this is totally unsafe,[/color]

That's a bit of an exaggeration. It's perfectly safe when used properly.
When used by programmers who don't bother to think it is, indeed,
totally unsafe.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Joe Hotchkiss
Guest
 
Posts: n/a
#7: Aug 22 '05

re: printf or something like it


"pillbug" <_pillbug@socal.rr.com> wrote in message
news:Y7pNe.4787$UE2.1882@tornado.socal.rr.com...[color=blue]
> hswerdfe wrote:[color=green]
> > is there a standard function that formats data the same way that[/color][/color]
printf[color=blue][color=green]
> > and sprintf do.
> >
> > only the return value is a char*/string of the formated values.
> >[/color]
>
> #include <cstdarg>
> char* mysprintf (char* buf, const char* fmt, ...)
> {
> va_list va;
> va_start (va, fmt);
> vsprintf (buf, fmt, va);
> va_end (va);
> return buf;
> }[/color]

Here's a variation on this theme that doesn't need a buffer to be passed
in. It uses a small pool of statically allocated buffers. Using a pool
of buffers rather than a single buffer means that it can safely be used
several times in a single statement. Adjust NumLines and the buffer
size to suit your taste/needs.

char* Format (const char* fmt, ...)
{
const int NumLines = 5;
static int Line = 0;
static char Text[NumLines][200];
va_list args;

va_start (args, fmt);
vsprintf (Text[Line], fmt, args);
va_end (args);

int n = Line;

++Line;
if (Line >= NumLines)
{
Line = 0;
}

return Text[n];
}

--
Regards,

Joe Hotchkiss,
http://joe.hotchkiss.com
http://harrowsubaqua.org.uk

XXXXXXXXXXXXXXXXXXXXXXXXX
X joe.hotchkiss X
X at selex-sas.com X
XXXXXXXXXXXXXXXXXXXXXXXXX



Closed Thread