So I'm abusing C++ in strange and wonderful ways, and I want to write a function like this:
Code:
void Log(char *filename, char *str, ...);
This function would open a file specified by filename and then call printf in the following sort of fashion:
Code:
fprintf(fp,str, ...);
The idea is to create a function that loads up a log file, printf's the stuff from str and ... to the log, and closes it, all nice and tidy. For example, you could do:
Code:
int a = 20;
Log("flarp.txt", "%d", a);
and "20" would end up in flarp.txt. Trouble is, I can't figure out how to take the variable number of args passed to the Log function and then pass those arguments from Log to printf. I tried the following, expecting that it wouldn't work:
Code:
#include <stdio.h>
#include <stdarg.h>
void printme(char * str, ...) {
va_list arglist;
va_start(arglist, str);
printf(str, arglist);
va_end(arglist);
};
int main () {
int a = 20;
printme("%d",a);
getchar();
return 0;
};
And the result was some random number, which I suspect was the address of the first argument in the "..." in "printme()". (EDIT: it is the value of "arglist", which I expect is a pointer). So, help, please?