Connecting Tech Pros Worldwide Help | Site Map

printf or something like it

  #1  
Old August 19th, 2005, 03:05 PM
hswerdfe
Guest
 
Posts: n/a
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

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

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

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

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

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
is it possible? Tinku answers 10 September 2nd, 2008 12:15 PM
nicely printf a matrix vectorizor answers 11 May 6th, 2007 05:35 AM
removing a printf statement from my program causes it to crash!? Harman Dhaliwal answers 14 May 28th, 2006 04:55 PM
constructing own error functions vs something like fprinf() grocery_stocker answers 5 November 15th, 2005 12:09 AM