wrapper printf function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rejeesh

    wrapper printf function

    Hi,

    I need a 'wrapper' function for printf. So that anywhere in my program
    if i call the wrapper function it does the same thing as the regular
    printf function, but puts a custom message at the begining.

    so here it goes :-

    int my_printf ( <what do I put here> )
    {
    printf("This is My Header::");
    printf( <Pass the arguments of the my_printf right here>);
    }

    and in some where in my main pgm files;

    my_printf("This is a test %d, %x, %s.\n", 10, 10, "10");

    should do exactly as printf does, except put "This is My Header::" at
    the begining.

    Please help!
    Thanks
  • Eric Sosman

    #2
    Re: wrapper printf function

    rejeesh wrote:[color=blue]
    >
    > Hi,
    >
    > I need a 'wrapper' function for printf. So that anywhere in my program
    > if i call the wrapper function it does the same thing as the regular
    > printf function, but puts a custom message at the begining.
    >
    > so here it goes :-
    >
    > int my_printf ( <what do I put here> )
    > {
    > printf("This is My Header::");
    > printf( <Pass the arguments of the my_printf right here>);
    > }
    >
    > and in some where in my main pgm files;
    >
    > my_printf("This is a test %d, %x, %s.\n", 10, 10, "10");
    >
    > should do exactly as printf does, except put "This is My Header::" at
    > the begining.[/color]

    #include <stdio.h>
    #include <stdarg.h>
    int my_printf(const char *format, ...) {
    int res1, res2;
    va_list ap;
    res1 = printf("This is My Header::");
    if (res1 < 0)
    return res1;
    va_start (ap, format);
    res2 = vprintf(format, ap);
    va_end (ap);
    return (res2 < 0) ? res2 : res1 + res2;
    }

    --
    Eric.Sosman@sun .com

    Comment

    • Ben Pfaff

      #3
      Re: wrapper printf function

      rejeesh@tenesix .com (rejeesh) writes:
      [color=blue]
      > I need a 'wrapper' function for printf. So that anywhere in my program
      > if i call the wrapper function it does the same thing as the regular
      > printf function, but puts a custom message at the begining.[/color]

      This is a FAQ.

      15.5: How can I write a function that takes a format string and a
      variable number of arguments, like printf(), and passes them to
      printf() to do most of the work?

      A: Use vprintf(), vfprintf(), or vsprintf().

      Here is an error() function which prints an error message,
      preceded by the string "error: " and terminated with a newline:

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

      void error(char *fmt, ...)
      {
      va_list argp;
      fprintf(stderr, "error: ");
      va_start(argp, fmt);
      vfprintf(stderr , fmt, argp);
      va_end(argp);
      fprintf(stderr, "\n");
      }

      See also question 15.7.

      References: K&R2 Sec. 8.3 p. 174, Sec. B1.2 p. 245; ISO
      Secs. 7.9.6.7,7.9.6.8 ,7.9.6.9; H&S Sec. 15.12 pp. 379-80; PCS
      Sec. 11 pp. 186-7.

      --
      "To get the best out of this book, I strongly recommend that you read it."
      --Richard Heathfield

      Comment

      Working...