Connecting Tech Pros Worldwide Help | Site Map

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

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 12:51 AM
winbatch
Guest
 
Posts: n/a
Default How do I write a function that accepts a 'printf' type parameter?

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, 12:51 AM
Jonathan Turkanis
Guest
 
Posts: n/a
Default 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, 12:51 AM
winbatch
Guest
 
Posts: n/a
Default 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, 12:51 AM
Jonathan Turkanis
Guest
 
Posts: n/a
Default 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, 12:51 AM
Joe Hotchkiss
Guest
 
Posts: n/a
Default 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, 12:53 AM
Winbatch
Guest
 
Posts: n/a
Default 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]


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

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 220,662 network members.