2017-02-28 18 views
-1

我試圖對一些沒有記錄的外部硬件進行一些調查。
對於這個我正在使用的可變參數在C.這是我自己的小記錄器是我的代碼:如何使用可變參數寫入WinCE小型記錄器

void write(const char* msg, ...) 
{ 
    va_list args; 
    va_start(args, msg); 

    FILE* file = fopen("/network/cewin/loggerfile.txt", "a"); 
    if(file != NULL) 
    { 
     vfprintf(file, msg, args); 
     fputc('\n', file); 
     fclose(file); 
     va_end(args); 
    } 
} 

首先,我測試在Windows 7的代碼,沒有問題,但在WinCE的不打印含參數的線。

下面是一個例子:

write("Hello World") - Works on Win 7 and WinCE 
write("Hello %s", "World") - Works on Win7, not on WinCE 
write("Hello %i", 5) - Works on Win7, not on WinCE 

我試圖理解爲什麼最後兩行是在Win7的工作,但不能在WinCE的。也許我需要使用別的東西比vfprintf

由於HW,我無法檢查方法的返回值。

+3

如果['fopen'(HTTPS://linux.die .net/man/3/fopen)失敗,那麼你應該檢查['errno'](https://linux.die.net/man/3/errno)以找出問題所在...... –

+0

我不' t認爲它失敗了,因爲如果我打印一個簡單的字符串:「Hello world」,它在WinCE上工作,文件被創建,但是如果我打印這個:(「Hello%s」,「World」),它不會加工。 – Kobe

+0

@MichaelWalz:它可以,它適用於簡單的字符串 – Kobe

回答

2

不是一個答案 - >我不能把代碼中的註釋 - '靜態' 調試功能,不使用變量參數或vnprintf

void debugI(char *str, int iValue) 
{ 
    FILE *debugPtr = fopen ("/debugpath/debugFile.txt", "a"); 
    fprintf (debugPtr, "debugI %s:%d\n", str, iValue); 
    fclose (debugPtr); 
} 
相關問題