开发者

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

void printLine(const wchar_t* str, ...) 
{
  // h开发者_如何学编程ave 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.


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.


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.


Typically one calls into a variable args version of the function, that accepts va_list. For example _snwprintf internally calls _vsnwprintf; try calling that.


Other people have already pointed you to the vprintf-family of functions, but this also (not surprisingly) is answered by the comp.lang.c FAQ, if you want to familiarize yourself with the other FAQ entries. (They're worth reading, IMO.)

How can I write a function that takes a format string and a variable number of arguments, like printf, and passes them to printf to do most of the work?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜