The code below compiles, runs, and gives correct results. Note, however, the commented out "Experiment" code. I want to be able to call a function which accepts a variable number of parameters, which then calls another function and passes it those parameters. How can I fix the commented-code so that it compiles and runs correctly? Is it possible to have the Experiment function be a simple one-line function as I am trying to do?
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void WriteMessageToFile (FILE* stream, char* message, ...)
{
}
void DLOG (char* filename, char* message, ...)
{
FILE* debugFile = fopen (filename, "a");
if (debugFile != 0)
{
va_list ap; /* points to each unnamed arg in turn */
char *p, *sval;
int ival;
double dval;
va_start(ap, message); /* make ap point to 1st unnamed arg */
for (p = message; *p; p++)
{
if (*p != '%')
{
fputc((int) *p, debugFile);
continue;
}
switch (*++p)
{
case 'd':
ival = va_arg(ap, int);
fprintf(debugFile, "%d", ival);
break;
case 'f':
dval = va_arg(ap, double);
fprintf(debugFile, "%f", dval);
break;
case 's':
for (sval = va_arg(ap, char *); *sval; sval++)
fputc((int) *sval, debugFile);
break;
default:
fputc((int) *p, debugFile);
break;
}
}
va_end(ap); /* clean up when done */
fprintf (debugFile, "\n");
fclose (debugFile);
}
}
/*
void Experiment (char* filename, char* message, ...)
{
DLOG (filename, message, ...);
}
*/
int main(int argc, char *argv[])
{
char* name1 = "Bob";
char* name2 = "Sam";
char* name3 = "Fred";
char* name4 = "Bill";
//Experiment ("SomeFile.txt", "Hello %s %s %s %s\n", name1, name2, name3, name4);
DLOG ("SomeFile.txt", "Hello %s %s %s %s\n", name1, name2, name3, name4);
return 0;
}