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

Discussion in 'C++' started by winbatch, Feb 14, 2005.

  1. winbatch

    winbatch Guest

    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.
     
    winbatch, Feb 14, 2005
    #1
    1. Advertising

  2. 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
     
    Jonathan Turkanis, Feb 14, 2005
    #2
    1. Advertising

  3. winbatch

    winbatch Guest

    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?
     
    winbatch, Feb 14, 2005
    #3
  4. 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
     
    Jonathan Turkanis, Feb 14, 2005
    #4
  5. "winbatch" <> wrote in message
    news:...
    > 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
     
    Joe Hotchkiss, Feb 14, 2005
    #5
  6. winbatch

    Winbatch Guest

    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" <> wrote in message
    news:42105f21$...
    > "winbatch" <> wrote in message
    > news:...
    >> 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
    >
    >
    >
     
    Winbatch, Feb 15, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Hansen
    Replies:
    1
    Views:
    388
    Joona I Palaste
    Aug 24, 2003
  2. titancipher
    Replies:
    4
    Views:
    515
    titancipher
    Jun 13, 2005
  3. Peter Hansen
    Replies:
    1
    Views:
    418
    Joona I Palaste
    Aug 24, 2003
  4. Michael B Allen

    printf parameter type wrangling

    Michael B Allen, Aug 21, 2005, in forum: C Programming
    Replies:
    15
    Views:
    649
    Richard Bos
    Aug 24, 2005
  5. Lampa Dario
    Replies:
    3
    Views:
    452
    Old Wolf
    Sep 10, 2005
Loading...

Share This Page