Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 19th, 2005, 03:05 PM
hswerdfe
Guest
 
Posts: n/a
Default printf or something like it

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?
  #2  
Old August 19th, 2005, 03:35 PM
Andre Kostur
Guest
 
Posts: n/a
Default 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.
  #3  
Old August 19th, 2005, 03:45 PM
Victor Bazarov
Guest
 
Posts: n/a
Default 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
  #4  
Old August 19th, 2005, 07:05 PM
pillbug
Guest
 
Posts: n/a
Default 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;
}
  #5  
Old August 19th, 2005, 07:25 PM
pillbug
Guest
 
Posts: n/a
Default 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;
}
  #6  
Old August 19th, 2005, 07:25 PM
Pete Becker
Guest
 
Posts: n/a
Default 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)
  #7  
Old August 22nd, 2005, 09:35 AM
Joe Hotchkiss
Guest
 
Posts: n/a
Default 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



 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles