[英]C: warning: format '%s' expects a matching 'char *' argument
I'm currently working on File I/O and I tried using this function: 我目前正在处理文件I / O,并尝试使用此功能:
fprintf (FILE *pFile, char *pFormat, <variable list>);
with my variables 与我的变量
fprintf ( filePtr, "\"%s\"n", myarray );
My compiler issues a warning that says warning: format '%s' expects a matching 'char *' argument
but I have declared myarray
as char *myarray = "This is a string"
. 我的编译器发出警告,
warning: format '%s' expects a matching 'char *' argument
但是我已将myarray
声明为char *myarray = "This is a string"
。 Can anyone tell me what went wrong? 谁能告诉我出了什么问题? My code is:
我的代码是:
#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;
}
I tried to compile with gcc, it found another issue - missing argument in the fopen: 我尝试使用gcc进行编译,但发现了另一个问题-fopen中缺少参数:
if ((filePtr = fopen("sample.dat")) == NULL) {
So, need to use: 因此,需要使用:
if ((filePtr = fopen("sample.dat", "r")) == NULL) {
Thereafter, code compiled without problem. 此后,代码编译没有问题。 Compiler version:
编译器版本:
gcc version 4.2.1 20070831 patched [FreeBSD]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.