2011-10-07 88 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

沒有看到代碼就看不出來。 –

相關問題