Connecting Tech Pros Worldwide Help | Site Map

How do I write a function that accepts a 'printf' type parameter?

  #1  
Old July 23rd, 2005, 01:51 AM
winbatch
Guest
 
Posts: n/a
Hi,
I'm trying to write a function that accepts a string of some kind
(either char * or std::string) that can be passed something in printf
semantics.

For example:

void test( string passedIn )
{
....etc..
}

test( "Whatever: %s = %d", val, 3 );

How would I write the test function to accept such a string so that it
can handle whatever is passed as a single string? (ie so that it will
get "Whatever: val = 3"

Thanks in advance.

  #2  
Old July 23rd, 2005, 01:51 AM
Jonathan Turkanis
Guest
 
Posts: n/a

re: How do I write a function that accepts a 'printf' type parameter?


winbatch wrote:[color=blue]
> Hi,
> I'm trying to write a function that accepts a string of some kind
> (either char * or std::string) that can be passed something in printf
> semantics.
>
> For example:
>
> void test( string passedIn )
> {
> ...etc..
> }
>
> test( "Whatever: %s = %d", val, 3 );
>
> How would I write the test function to accept such a string so that it
> can handle whatever is passed as a single string? (ie so that it will
> get "Whatever: val = 3"
>
> Thanks in advance.[/color]

If you really want to do this, declare your function like so

void test(std:string, ...);

Then, in the implementation of string, use the macros and data types from
<stdarg.h> (or <cstdarg>) to access the variable argument list.

However, functions with variable argument lists are hard to implement and use
correctly, since the are not type-safe. I'd recommend trying to find another
solution.

Jonathan



  #3  
Old July 23rd, 2005, 01:51 AM
winbatch
Guest
 
Posts: n/a

re: How do I write a function that accepts a 'printf' type parameter?


I thought of doing it this way, but I thought that the 'variable'
parameters were all of the same datatype as the 'known' parameter. (If
I'm right), this wouldn't work since it could be passed integers, etc.

Anybody have a different idea?

  #4  
Old July 23rd, 2005, 01:51 AM
Jonathan Turkanis
Guest
 
Posts: n/a

re: How do I write a function that accepts a 'printf' type parameter?


winbatch wrote:[color=blue]
> I thought of doing it this way, but I thought that the 'variable'
> parameters were all of the same datatype as the 'known' parameter.[/color]

No, they can be any POD type. I think in Java they all have to have the same
type.
[color=blue]
> (If I'm right), this wouldn't work since it could be passed integers,
> etc.
>
> Anybody have a different idea?[/color]

Another approach is illustrated by Boost.Format:

http://www.boost.org/libs/format/index.html

Jonathan


  #5  
Old July 23rd, 2005, 01:51 AM
Joe Hotchkiss
Guest
 
Posts: n/a

re: How do I write a function that accepts a 'printf' type parameter?


"winbatch" <hoffmandan@gmail.com> wrote in message
news:1108341643.590006.293000@o13g2000cwo.googlegr oups.com...[color=blue]
> Hi,
> I'm trying to write a function that accepts a string of some kind
> (either char * or std::string) that can be passed something in printf
> semantics.
>
> For example:
>
> void test( string passedIn )
> {
> ...etc..
> }
>
> test( "Whatever: %s = %d", val, 3 );
>
> How would I write the test function to accept such a string so that it
> can handle whatever is passed as a single string? (ie so that it will
> get "Whatever: val = 3"[/color]

#include <stdarg.h>
#include <stdio.h>

int test (const char* fmt, ...)
{
va_list args;
int count;

va_start (args, fmt);
count = vprintf (fmt, args);
va_end (args);

return count;
}

int main ()
{
test ("Whatever: %s = %d\n", "val", 3);

return 0;
}

vsprintf and vfprintf work similarly.

--
Regards,

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

XXXXXXXXXXXXXXXXXXXXXXXXX
X joe.hotchkiss X
X at baesystems.com X
XXXXXXXXXXXXXXXXXXXXXXXXX



  #6  
Old July 23rd, 2005, 01:53 AM
Winbatch
Guest
 
Posts: n/a

re: How do I write a function that accepts a 'printf' type parameter?


Joe,
Thanks for your help. I ended up using vsnprintf which worked out great.
( I wanted to store the converted string and then write out the log file,
not just print it out like vprintf did).

Dan
"Joe Hotchkiss" <nospam@baesystems.com> wrote in message
news:42105f21$1_1@baen1673807.greenlnk.net...[color=blue]
> "winbatch" <hoffmandan@gmail.com> wrote in message
> news:1108341643.590006.293000@o13g2000cwo.googlegr oups.com...[color=green]
>> Hi,
>> I'm trying to write a function that accepts a string of some kind
>> (either char * or std::string) that can be passed something in printf
>> semantics.
>>
>> For example:
>>
>> void test( string passedIn )
>> {
>> ...etc..
>> }
>>
>> test( "Whatever: %s = %d", val, 3 );
>>
>> How would I write the test function to accept such a string so that it
>> can handle whatever is passed as a single string? (ie so that it will
>> get "Whatever: val = 3"[/color]
>
> #include <stdarg.h>
> #include <stdio.h>
>
> int test (const char* fmt, ...)
> {
> va_list args;
> int count;
>
> va_start (args, fmt);
> count = vprintf (fmt, args);
> va_end (args);
>
> return count;
> }
>
> int main ()
> {
> test ("Whatever: %s = %d\n", "val", 3);
>
> return 0;
> }
>
> vsprintf and vfprintf work similarly.
>
> --
> Regards,
>
> Joe Hotchkiss,
> http://joe.hotchkiss.com
> http://harrowsubaqua.org.uk
>
> XXXXXXXXXXXXXXXXXXXXXXXXX
> X joe.hotchkiss X
> X at baesystems.com X
> XXXXXXXXXXXXXXXXXXXXXXXXX
>
>
>[/color]


Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
K&R2 section 2.7 type conversions (exercise) arnuld answers 12 March 18th, 2007 05:55 AM
comp.lang.c Answers (Abridged) to Frequently Asked Questions (FAQ) Steve Summit answers 0 November 13th, 2005 06:45 PM
comp.lang.c Answers (Abridged) to Frequently Asked Questions (FAQ) Steve Summit answers 0 November 13th, 2005 05:26 PM
comp.lang.c Answers (Abridged) to Frequently Asked Questions (FAQ) Steve Summit answers 0 November 13th, 2005 03:16 AM