passing a variable number of arguments to another function

I want to use a function to log error messages. This function should open a log file and use fprintf to write to it. But I don't know how I should pass the arguments. For example:

log_error("Error at line %d at position %d\n",line,pos);

This means:

f = fopen(logfile,"a");
fprintf(f,"Error at line %d at position %d\n",line,pos);
fclose(f);

Is this possible (without using macro's)?
you mean you want to write a function like printf???

read about va_list, va_start.. etc .
I know how to pass a variable number of arguments, but I don't know how I can pass these arguments to fprintf.

EDIT: I found the solution. I had to use vfprintf, it is identical to fprintf but it uses a va_list as the third argument.

1
2
3
4
5
6
7
void WriteFormatted (FILE * stream, char * format, ...)
{
  va_list args;
  va_start (args, format);
  vfprintf (stream, format, args);
  va_end (args);
}
Uhh... Wouldn't it have been faster to #define WriteFormatted fprintf ?
That was the example code for vfprintf. The real function is more complicated.
Last edited on
Topic archived. No new replies allowed.