Results 1 to 2 of 2

Thread: printf like function

  1. #1

    printf like function

    how do i make a function that gets a format and any number of arguments and then puts them together? like the kernel has printk i need a error() and die() that use printf format but do some different things.

    thanks

  2. #2

    Re:printf like function

    What you need is a variadic function, together with vprintf (unless you really want to parse the format string yourself). Here is an example they I have shamelessly ripped from the GNU libc manual which seems to be along similar lines as to what you want to do.
    Code:
         #include <stdio.h>
         #include <stdarg.h>
         
         void
         eprintf (const char *template, ...)
         {
           va_list ap;
           extern char *program_invocation_short_name;
         
           fprintf (stderr, "%s: ", program_invocation_short_name);
           va_start (ap, count);
           vfprintf (stderr, template, ap);
           va_end (ap);
         }
    Example usage from libc manual:
    Code:
           eprintf ("file `%s' does not exist\n", filename);
    starg.h gives you the macros required to deal with variable numbers of arguments.
    The "..." in the function definition defines the function as having a variable number of arguments from that point.
    va_start sets up a pointer to the first argument.
    vfprintf uses the format string supplied plus the argument pointer to do the business.
    va_end signifies that the argument list is finished with.
    I imagine that program_invocation_shortname thing needs sterr.h.

Similar Threads

  1. TRIM Function
    By jody in forum Windows - General Topics
    Replies: 6
    Last Post: 03-01-2005, 06:18 PM
  2. Excel VLOOKUP Function
    By swatsp0p in forum Windows - General Topics
    Replies: 1
    Last Post: 01-26-2005, 06:21 PM
  3. EVALUATE() Function with INDIRECT() Returns #REF
    By regix in forum Windows - General Topics
    Replies: 0
    Last Post: 01-04-2005, 01:39 AM
  4. C++: pass filename to function
    By Blaqb0x in forum Linux - Software, Applications & Programming
    Replies: 2
    Last Post: 09-03-2002, 04:37 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •