2013-05-03 42 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有太多的字符。它解释了警告和注释。