如何修复不兼容的指针类型传递' char [16]'参数类型' FILE *' (又名' struct __sFILE *')[ - 兼容指针类型]

时间:2018-02-15 13:58:16

标签: c

如何修复?

  

警告:

     

不兼容的指针
           类型传递' char [16]'参数类型' FILE *'
           (又名' struct __sFILE *')[ - 兼容指针类型]
         的fread(缓冲器,1512,数据);
                         ^ ~~~
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/
  SDKs / MacOSX10.13.sdk / usr / include / stdio.h:247:90:注意:
       将参数传递给参数' __ stream'这里
   ...__ ptr,size_t __size,size_t __nitems,FILE * __restrict __stream);
                                                                ^
  生成了1个警告。

这是我的代码:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

int main()
{

    char readDATA[64];
    char buffer[8];
    char data[16];

    FILE *fl = fopen(data,"r");
    fgets(readDATA,64,fl);
    fread(buffer,1,512,data);
    printf("%s",readDATA);

    return 0;
}

我尝试打开一条路径或随机播放&#34;请帮忙。

1 个答案:

答案 0 :(得分:3)

根据fread()

的手册页
  

函数fread()读取数据的nmemb元素,每个字节大小          从流指向的流,将它们存储在位置          由ptr。

给出
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);

fread()的最后一个参数是FILE *stream,表示您想要读取内容并存储到buffer的文件。

替换

 fread(buffer,1,512,data); /* last argument is wrong */

fread(buffer,1,512,f1);

同时检查fopen()

的返回值
FILE *f1 = fopen("data","r"); /* it will try to open a file called "data" in current working directory, you can take input from user also */
if(f1 == NULL) {
  fprintf(stderr, "file not present\n");
  return 0;
}

你的意图可能是这样的

char data[16];/* it doesn't contain anything, so take the input from user or assign directly */
printf("enter the file name\n");
scanf("%s",data);
FILE *f1 = fopen(data,"r"); 
    if(f1 == NULL) {
      fprintf(stderr, "file not present\n");
      return 0;
    }