Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass varargs to printf [duplicate]

I'd like to have a helper function log that essentially does the following:

log(file, "array has %d elements\n", 10);
// writes "2014-02-03 16:33:00 - array has 10 elements" to &file

I have the time portion down, and I have the file writing portion down. However, the problem is the method signature itself for log — what should I put? This says that the printf declaration ends with the ... keyword, but how can I use this in my function?

void log(FILE *f, const char * format, ...) // how would I pass ... to fprintf?

Let me EDIT this to include a bit more information.

I have a const char * now () that returns a string of the form "2014-02-03 16:33:00." I would like to pass another format string in like this. The two statements should be equivalent:

log(file, "array has %d elements\n", 10);
fprintf(file, "%s - array has %d elements\n", now(), 10);

I know that vfprintf allows me to pass a va_list, but how can I put the now() as the first argument, before all the others?

like image 766
wchargin Avatar asked Feb 04 '14 00:02

wchargin


People also ask

Can you pass va_list to another function?

The va_list may be passed as an argument to another function, but calling va_arg() within that function causes the va_list to have an indeterminate value in the calling function. As a result, attempting to read variable arguments without reinitializing the va_list can have unexpected behavior.

How do you pass variable arguments in C++?

Variable number of arguments in C++Define a function with its last parameter as ellipses and the one just before the ellipses is always an int which will represent the number of arguments. Create a va_list type variable in the function definition. This type is defined in stdarg. h header file.

What is Varargs C?

ISO C defines a syntax for declaring a function to take a variable number or type of arguments. (Such functions are referred to as varargs functions or variadic functions.)


2 Answers

Use vprintf, which is declared as:

int vprintf(const char *format, va_list ap);

In your log function, invoke va_start to obtain a va_list value, then pass that value to vprintf.

Since your log function takes a FILE* argument, you'll probably want to use vfprintf rather than vprintf (and perhaps update your question to ask about fprintf rather than printf).

Incidentally, you might want to reconsider using the name log; that's the name of a standard function declared in <math.h>.

Reflecting your updated question, you can print the timestamp inside log by calling fprintf directly:

va_list(args);
fprintf(f, "%s - ", now());
va_start(args, format);
vfprintf(f, format, args);
like image 163
Keith Thompson Avatar answered Sep 30 '22 17:09

Keith Thompson


The method you are looking for is vfprintf or possible vprintf (unclear which by your question)

  • http://www.cplusplus.com/reference/cstdio/vfprintf/
  • http://www.cplusplus.com/reference/cstdio/vprintf/

This is essentially the implementation of printf that allows a va_list to be explicitly passed in as a parameter. So in your method you can create the va_list for your method parameters and forward it onto vfprintf

void log(FILE* f, const char* format, ...) { 
  va_list args;
  va_start (args, format);
  vfprintf (f, format, args);
  va_end (args);
}
like image 27
JaredPar Avatar answered Sep 30 '22 16:09

JaredPar