Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How do I write a function that accepts a 'printf' type parameter?

Reply
Thread Tools

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

 
 
winbatch
Guest
Posts: n/a
 
      02-14-2005
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.

 
Reply With Quote
 
 
 
 
Jonathan Turkanis
Guest
Posts: n/a
 
      02-14-2005
winbatch wrote:
> 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



 
Reply With Quote
 
 
 
 
winbatch
Guest
Posts: n/a
 
      02-14-2005
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?

 
Reply With Quote
 
Jonathan Turkanis
Guest
Posts: n/a
 
      02-14-2005
winbatch wrote:
> 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


 
Reply With Quote
 
Joe Hotchkiss
Guest
Posts: n/a
 
      02-14-2005
"winbatch" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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



 
Reply With Quote
 
Winbatch
Guest
Posts: n/a
 
      02-15-2005
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" <(E-Mail Removed)> wrote in message
news:42105f21$(E-Mail Removed)...
> "winbatch" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
>> 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
>
>
>



 
Reply With Quote
 
 
 
Reply

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
RemoveNodes function - accepts XPath Hoss XML 6 01-07-2007 10:10 PM
Making a class that accepts a user-defined STL coparison function titancipher C++ 4 06-13-2005 07:51 AM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM



Advertisments