2017-08-09 128 views
-2

我有一個文件a.ca.h,它用作其他文件中功能的支持模塊。文件指針未定義的錯誤

在這些函數中,我必須打開一個文件並執行讀取數據,寫入數據,關閉它等功能。

出於這些目的,我把文件指針的全局聲明中a.c通過

static FILE* pFile; 

,並直接使用pFile

編譯器被扔錯誤的錯誤是這樣的:

"pFile" not defined in the function 

有什麼不對嗎?

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <sys/types.h> 
#include <sys/socket.h> 
#include <unistd.h> 
#include <errno.h> 
#include <fcntl.h> 
#include <arpa/inet.h> 

#define path "/tmp/diag.log" 

FILE* pFile; 

void a(bool value) { 
    if (value) { 
      pFile = fopen (path,"a"); 
     return; 
    } 
    else if (!value) { 
     fclose (pFile); 
    } 
} 

void b(bool value) { 
    if(value) 
    { 
    fprintf (pFile,"%s",message); 
    } 
    } 

更新: 錯誤是我的makefile從build目錄中取文件和變化沒有得到reflected.Thanks很多關於你的幫助

+1

你明白的需求和影響靜態存儲類? –

+0

hehe ...這是一個錯誤..在這 – Ram

+1

幫助我請[請閱讀如何問好問題](http://stackoverflow.com/help/how-to-ask)。然後編輯您的問題以包含[最小,完整和可驗證示例](http://stackoverflow.com/help/mcve),以及實際的錯誤消息(完整,完整且無需修改)作爲文本。 –

回答

0

如果您嘗試使用此static FILE* pFile從另一個編譯單位去除static

在其他文件中添加:

extern FILE *pfile; 
+0

謝謝你的答覆...我已經刪除靜態,但它仍然是拋出pFile未定義的錯誤..我已經聲明File * pFile在宏下的同一個ac中,這個文件只包含函數...我應該在其他地方聲明嗎?再次感謝你的回覆 – Ram

+1

@Ram顯示更多代碼 –

+0

嗨..我已經添加了類似我的實現您的友善細讀的代碼..我應該添加在調用此功能的文件...我不能夠理解,因爲這應該是本地文件儀式? – Ram