Re: question related to passing variable number of arguments



On Wed, 19 Sep 2007 00:55:35 -0700, "junky_fellow@xxxxxxxxxxx"
<junky_fellow@xxxxxxxxxxx> wrote:

Hi,

I want to define a function log_file(), that would print the
formatted message to stdout if the FILE pointer passed is NULL else
print the formatted output to the file corresponding to FILE*.

For eg.

extern FILE *fp;

int main(void)
{
int n1=0;
int n2=100;

log_file(fp, "SOME MESSAGE n1=%d n2=%d\n", n1, n2);
}

I want that, log_file() should print the formatted output to stdout,
in case fp is NULL, else
print the output to the corresponding file.
Can some one please help ? I tried my best, but could not find any
solution.

thanks a lot for any help.

Hi junky fellow,

The following doesn't deal with your extern fp, because it represents
a single translation unit that compiles and links. Nevertheless, I
hope it gives you an idea of how you can solve your problem.

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

#define MAX_MSG_LENGTH 256 /*or whatever size you need*/

static FILE *G_fp = NULL;
static void log_file(FILE *fp, const char *format, ...);

int main(void)
{
int n1 = 0;
int n2 = 100;

log_file(G_fp, "SOME MESSAGE n1 = %d and n2 = %d\n", n1, n2);
return 0;
}

static void log_file(FILE *fp, const char *format, ...)
{
va_list arg_ptr;
char text[MAX_MSG_LENGTH + 1];

va_start(arg_ptr, format);
vsprintf(text, format, arg_ptr);
va_end(arg_ptr);
if ( fp )
{
fprintf(fp, "%s", text);
}
else
{
fprintf(stdout, "%s", text);
}
}

Regards
--
jay
.



Relevant Pages

  • [PATCH] new bitmap list format (for cpusets)
    ... A bitmap print and parse format that provides lists of ranges of ... The intention is to provide a format for the cpu and memory mask files ... extern int bitmap_parse(const char __user *ubuf, unsigned int ulen, ...
    (Linux-Kernel)
  • Re: [Lse-tech] [PATCH] new bitmap list format (for cpusets)
    ... came to the conclusion that the basic list format, ... So I redid the bitmap list format patch, ... A bitmap print and parse format that provides lists of ranges of ... extern int bitmap_parse(const char __user *ubuf, unsigned int ulen, ...
    (Linux-Kernel)
  • [PATCH] new bitmap list format (for cpusets)
    ... [This is a copy of the bitmap list format patch that I submitted to ... A bitmap print and parse format that provides lists of ranges of ... extern int bitmap_parse(const char __user *ubuf, unsigned int ulen, ...
    (Linux-Kernel)
  • Re: Using References to Formats, Examining Scalars With Devel::Peek
    ... the format to be able to refer to it by another name didn't occur to me. ... Seems most of the fields are the same as for the rest of the Perl ... from integer to double and how frequently the string portions are updated. ... Using an operator or a built-in with a double and an int usually converts ...
    (comp.lang.perl.misc)
  • Re: question related to passing variable number of arguments
    ... formatted message to stdout if the FILE pointer passed is NULL else ... print the formatted output to the file corresponding to FILE*. ... int n2=100; ... static void log_file(FILE *fp, const char *format, ...) ...
    (comp.lang.c)