How to get all arguments from following function in c/c++?

冷暖自知 提交于 2019-12-02 14:51:16

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.

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);

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!