Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to pass a C ellipsis call through directly?

Tags:

c++

c

void printLine(const wchar_t* str, ...) 
{
  // have to do something to make it work
  wchar_t buffer[2048];        
  _snwprintf(buffer, 2047, ????);
  // work with buffer
}

printLine(L"%d", 123);

I tried

  va_list vl;
  va_start(vl,str);

and things like this but I didn't find a solution.

like image 740
Totonga Avatar asked Feb 01 '10 14:02

Totonga


2 Answers

Here's a simple C code that does this, you will have to include stdarg.h for this to work.

void panic(const char *fmt, ...){
   char buf[50];

   va_list argptr; /* Set up the variable argument list here */

   va_start(argptr, fmt); /* Start up variable arguments */

   vsprintf(buf, fmt, argptr); /* print the variable arguments to buffer */

   va_end(argptr);  /* Signify end of processing of variable arguments */

   fprintf(stderr, buf); /* print the message to stderr */

   exit(-1);
}

The typical invocation would be

panic("The file %s was not found\n", file_name); /* assume file_name is "foobar" */
/* Output would be: 

The file foobar was not found

*/

Hope this helps, Best regards, Tom.

like image 92
t0mm13b Avatar answered Sep 17 '22 16:09

t0mm13b


What you want to use is vsprintf it accepts the va_list argument and there is sample code on MSDN in the link.

EDIT: You should consider _vsnprintf which will help avoid buffer overrun issues that vsprintf will happily create.

like image 29
Ruddy Avatar answered Sep 20 '22 16:09

Ruddy