简体   繁体   English

C:警告:格式'%s'需要匹配的'char *'参数

[英]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.

相关问题 警告:格式“%s”需要匹配的“char *”参数 [-Wformat=] - warning: format ‘%s’ expects a matching ‘char *’ argument [-Wformat=] 数组和“警告:格式&#39;%s&#39;需要匹配的&#39;char *&#39;参数[-Wformat =]” - Array and “warning: format '%s' expects a matching 'char *' argument [-Wformat=]” 在 C 中,如何修复此警告:格式“%s”需要“char *”类型的参数,但参数 3 的类型为“char (*)[100] - In C, how to fix this warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘char (*)[100] 警告:格式“%s”需要类型“char *”,但参数 2 的类型为“char (*)” - warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)’ 警告:格式&#39;%s&#39;需要类型&#39;char *&#39;,但参数2的类型为&#39;int&#39; - warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’ 警告:格式%s期望为char *类型,但参数2为int类型 - warning: format %s expects type char * but argument 2 has type int 警告:格式“%s”在简单程序中需要 char 类型的参数问题 - warning: format '%s' expects argument of type char problem in a simple program 警告:格式&#39;%c&#39;需要类型&#39;int&#39;,但参数2的类型为&#39;char *&#39; - warning: format '%c' expects type 'int', but argument 2 has type 'char *' 在C中-警告:格式&#39;%d&#39;需要匹配的&#39;int&#39;参数[-Wformat] - In C - warning: format '%d' expects a matching 'int' argument [-Wformat] 警告:格式&#39;%s&#39;期望的参数类型为&#39;char *&#39;,但是参数2的类型为&#39;char&#39; - warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM