How to get all arguments from following function i

2020-05-10 08:49发布

following is the implementation of my method

static VALUE myMethod(VALUE self, VALUE exc, const char* fmt, ...) { 
   // Need to get all the arguments passed to this function and print it 
}

function is called as follows:

myMethod(exception, ""Exception message: %s, Exception object %d",
          "Hi from Exception", 100);

Can you provide the code for myMethod() that will access all the arguments and print them out.

Thanks in advance.

3条回答
成全新的幸福
2楼-- · 2020-05-10 09:11

The va_start and va_arg macro's are used to get the variable arguments in a function. An example can be found on the Microsoft site: http://msdn.microsoft.com/en-us/library/kb57fad8(v=vs.71).aspx

In your case it's a bit trickier, since you need to parse the format string to exactly know how many arguments should be given and of which type they are. Luckily, the CRT contains a function for that. The vfprintf function can be given a va_list (which you get from va_start). vfprintf will use this one to process all the extra arguments. See http://www.cplusplus.com/reference/clibrary/cstdio/vfprintf/ for an example.

查看更多
做个烂人
3楼-- · 2020-05-10 09:25

One way is to use vsnprintf().

Sample code:

char buf[256];
va_list args;

va_start(args, fmt);

if(vsnprintf(buf, sizeof(buf), fmt, args) > 0)
  fputs(buf, stderr);

va_end(args);
查看更多
家丑人穷心不美
4楼-- · 2020-05-10 09:28

You need to use va_start and va_arg macros to get the arguments. You can take a look at this - it has some examples.

http://www.go4expert.com/forums/showthread.php?t=17592

查看更多
登录 后发表回答