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

W

winbatch

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.
 
J

Jonathan Turkanis

winbatch said:
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.

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
 
W

winbatch

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?
 
J

Jonathan Turkanis

winbatch said:
I thought of doing it this way, but I thought that the 'variable'
parameters were all of the same datatype as the 'known' parameter.

No, they can be any POD type. I think in Java they all have to have the same
type.
(If I'm right), this wouldn't work since it could be passed integers,
etc.

Anybody have a different idea?

Another approach is illustrated by Boost.Format:

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

Jonathan
 
J

Joe Hotchkiss

winbatch said:
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"

#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
 
W

Winbatch

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,652
Latest member
Campbellamy

Latest Threads

Top