2011-10-07 85 views
1

我试图用以下方式使用vfprintf
vfprintf(fp,buffer,arg);在没有找到vfprintf来源:我创建一个使用运行的日期和时间,缓冲区和ARGSC vfprintf函数不能与字符串参数一起使用

fp = fopen(filename, "a"); 
va_start(arg, message); 
vprintf(message, arg); // print stdout 
vfprintf(fp, buffer, arg); // print to file 
va_end(arg); 
fclose(fp); 

它完全用数字和它的错误可怕的死亡其余 FP日志文件。 ...

有没有人有任何想法我做错了任何机会?

回答

3

您可能正在使用64位体系结构,其中可变长度arg只能传递一次。为了让更多的传球复制arg使用va_copy()

va_list arg, arg2; 
va_start(arg, message); 
va_copy(arg2, arg); 
vprintf(message, arg); // print stdout 
va_end(arg); 
vfprintf(fp, buffer, arg2); // print to file 
va_end(arg2); 

man va_arg

The va_arg() macro expands to an expression that has the type and value of the next argument in the call. The argument ap is the va_list ap initialized by va_start(). Each call to va_arg() modifies ap so that the next call returns the next argument. The argument type is a type name specified so that the type of a pointer to an object that has the specified type can be obtained simply by adding a * to type.

...

If ap is passed to a function that uses va_arg(ap,type) then the value of ap is undefined after the return of that function.

+0

你好,谢谢!它似乎在工作。你如何解释一个事实,如果你把一个数字而不是字符串,它的工作原理?它也应该两次返回数字,不是吗? – Laurence

+0

没有看到代码就看不出来。 –

相关问题