2013-05-03 122 views
0

我得到這些編譯警告,在我的功能並不能找到我的代碼的任何錯誤:編寫文件時編譯警告,最新錯誤?

警告:

src.c: In function ‘writeFiles’: 
src.c:119:18: warning: character constant too long for its type [enabled by default] 
src.c:119:2: warning: passing argument 1 of ‘fopen’ makes pointer from integer without a cast [enabled by default] 
/usr/include/stdio.h:269:14: note: expected ‘const char * __restrict__’ but argument is of type ‘int’ 

我的功能:

FILE *outfile; 
int writeFiles(int pwm, int rpm) 
{ 

    outfile = fopen('/dev/fan/rpm', "w+"); /* line 119 with the warnings */ /*create the new file */ 
    fprintf(outfile, "%d", rpm);   /*write the rpm to the file */ 
    fclose(outfile);      /*close the file */ 

    return (0); 

} 

我希望你能幫助我解決這個問題。 google和stackoverflow都不能幫助找到解決這個問題的辦法。

+0

你能在你的代碼片段中標出給出警告的行號嗎?另外'fopen'需要一個char *作爲第一個參數,爲什麼你的分隔符是單引號? – Ryan 2013-05-03 19:28:04

+1

'''字符是你的問題。僅爲單個字符使用單引號。對於字符串使用雙引號。 – 2013-05-03 19:30:23

+2

''/ dev/fan/rpm'' --->'「/ dev/fan/rpm」' – BLUEPIXY 2013-05-03 19:31:10

回答

2

警告發生是因爲fopen需要一個char *作爲參數,但是您將它傳遞給一個(太長的)char。

fopen("/dev/fan/rpm", "w+")代替fopen('/dev/fan/rpm', "w+")Double報價。

+0

謝謝,這個問題解決了,我會盡快接受你的回答。 (9分鐘) – 2013-05-03 19:32:14

+0

很高興聽到它是固定的:) – Ryan 2013-05-03 19:32:31

+1

實際上,他傳遞了一個'int',而不是'char'。像'''或''''這樣的整數字符常量的類型是'int'。 – 2013-05-03 19:34:56

0

我相信你需要在fopen調用的文件名周圍加雙引號。

例如

outfile = fopen("/dev/fan/rpm", "w+"); 
1

萊恩指出你的文件名實際上是一個char有太多的字符。它解釋了警告和註釋。