【发布时间】:2013-03-22 04:59:16
【问题描述】:
我正在学习 C,我来自 Java 背景。如果我能得到一些指导,我将不胜感激。这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main(void)
{
char *str = "test text\n";
FILE *fp;
fp = fopen("test.txt", "a");
write(fp, str);
}
void write(FILE *fp, char *str)
{
fprintf(fp, "%s", str);
}
当我尝试编译时,我得到了这个错误:
xxxx.c: In function ‘main’:
xxxx.c:18: warning: passing argument 1 of ‘write’ makes integer from pointer without a cast
/usr/include/unistd.h:363: note: expected ‘int’ but argument is of type ‘struct FILE *’
xxxx.c:18: error: too few arguments to function ‘write’
xxxx.c: At top level:
xxxx.c:21: error: conflicting types for ‘write’
/usr/include/unistd.h:363: note: previous declaration of ‘write’ was here
有什么想法吗?感谢您的宝贵时间。
【问题讨论】:
-
我认为您正在调用系统调用
write()。或许在main()之上包含一个原型,更好的是,选择一个系统调用未使用的名称。 -
你的函数没有问题,问题是选择的名称(unistd.h中使用了“write”)pubs.opengroup.org/onlinepubs/009695399/functions/write.html
-
谢谢大家,对不起,让我们度过了一个漫长的夜晚。
-
我认为你也可以使用 fwrite(str, 1, strlen(str), fp)。虽然效率不高。