Re: question related to passing variable number of arguments
- From: jaysome <jaysome@xxxxxxxxxxx>
- Date: Wed, 19 Sep 2007 01:35:51 -0700
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
.
- Follow-Ups:
- Re: question related to passing variable number of arguments
- From: Tor Rustad
- Re: question related to passing variable number of arguments
- From: Eric Sosman
- Re: question related to passing variable number of arguments
- From: junky_fellow@xxxxxxxxxxx
- Re: question related to passing variable number of arguments
- References:
- question related to passing variable number of arguments
- From: junky_fellow@xxxxxxxxxxx
- question related to passing variable number of arguments
- Prev by Date: Re: tryick use of sizeof again[Explaination Wanted]
- Next by Date: Re: Another sizeof question
- Previous by thread: question related to passing variable number of arguments
- Next by thread: Re: question related to passing variable number of arguments
- Index(es):
Relevant Pages
|
|