【发布时间】:2013-10-11 11:30:30
【问题描述】:
我目前正在处理文件 I/O,并尝试使用此功能:
fprintf (FILE *pFile, char *pFormat, <variable list>);
我的变量
fprintf ( filePtr, "\"%s\"n", myarray );
我的编译器发出警告,上面写着warning: format '%s' expects a matching 'char *' argument,但我已将myarray 声明为char *myarray = "This is a string"。谁能告诉我出了什么问题?我的代码是:
#include <stdio.h>
int main ()
{
FILE *filePtr;
char *myarray = "This is a string";
if ((filePtr = fopen("sample.dat", "w")) == NULL) {
printf ("File could not be opened.\n");
} else {
printf ("This will print the string onto file:\n");
while (!feof(filePtr)) {
fprintf ( filePtr, "\"%s\"", myarray );
}
}
fclose (filePtr);
return 0;
}
【问题讨论】:
-
没有。显示实际代码。
-
为什么在格式字符串中使用反斜杠?
-
向我们展示实际代码。并给我们完整的编译器输出。
-
Krister,他正在尝试打印一个包含在(文字)双引号中的字符串。这没有什么问题。
-
请注意,您需要在写入模式下
fopen()文件。此外,while循环将永远不会终止。使用feof()表示您正在读取而不是写入的文件的结尾。