question related to passing variable number of arguments

J

junky_fellow

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.
 
J

jaysome

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
 
J

junky_fellow

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*.
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);
}

}

Thanks a lot, jaysome. This really solved the problem.
 
B

Barry Schwarz

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.

int logfile(FILE *f, ...){
/* after processing argument list */
if (f == NULL) f = stdout;
return fprintf(...);}

The real issue is what you are going to do with the format string and
the subsequent arguments. Do you really intend to generate your own
code to duplicate the functionality of the *printf functions?

You could make life easy on yourself and use sprintf to perform all
the hard work and then use the much simpler

int logfile(FILE *f, const char *s){
if (f == NULL) f = stdout;
return fputs(s, f);}

At this point, logfile would hardly merit a function and could be
replaced by a macro.


Remove del for email
 
C

CBFalconer

.... snip ...

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.

In log_file():

if (NULL == fp) fp = stdout;
....
 
E

Eric Sosman

jaysome said:
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);
}
}

Ugh. Ugh, ugh, ugh. Use vfprintf() and get rid of
the perilous fixed-size buffer:

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

static /* depending on circumstances */
void log_file(FILE *fp, const char *format, ...)
{
va_list arg_ptr;
va_start(arg_ptr, format);
vfprintf (fp == NULL ? stdout : fp, format, arg_ptr);
va_end(arg_ptr);
}
 
F

Fred Kleinschmidt

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.

Make it real simple:

#define OUTFILE(f) ((f)?(f):stdout)

then to log:
fprintf( OUTFILE(fp), format, <arguments> );
 
T

Tor Rustad

jaysome said:
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);

YABOB... KABOOOM!

Better:

static void vfile_log(FILE *flog, const char *message, va_list argp);

void file_log(FILE *flog, const char *msg, ...)
{
va_list argp;

assert(msg != NULL);

va_start(argp, msg);
vfile_log(flog, msg, argp);
va_end(argp);
}

static void vfile_log(FILE *flog, const char *message, va_list argp)
{
....

if (flog && message)
{
(void) vfprintf(flog, message, argp);
(void) fprintf(flog, "\n");
fflush(flog);
}
....
}
 
J

jaysome

jaysome said:
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);
}
}

Ugh. Ugh, ugh, ugh. Use vfprintf() and get rid of
the perilous fixed-size buffer:

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

static /* depending on circumstances */
void log_file(FILE *fp, const char *format, ...)
{
va_list arg_ptr;
va_start(arg_ptr, format);
vfprintf (fp == NULL ? stdout : fp, format, arg_ptr);
va_end(arg_ptr);
}

Indeed. Using vfprintf is better than using vsprintf in the instant
case. The OP should heed your advice.

Thanks
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top