简体   繁体   English

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

[英]how to fix incompatible pointer types passing 'char [16]' to parameter of type 'FILE *' (aka 'struct __sFILE *') [-Wincompatible-pointer-types]

how to fix? 怎么修?

warning: 警告:

incompatible pointer 不兼容的指针
types passing 'char [16]' to parameter of type 'FILE *' 类型,将'char [16]'传递给'FILE *'类型的参数
(aka 'struct __sFILE *') [-Wincompatible-pointer-types] (aka'结构__sFILE *')[-Wincompatible-pointer-types]
fread(buffer,1,512,data); fread(缓冲区,1,512,数据);
^~~~ ^ ~~~
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/ /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/
SDKs/MacOSX10.13.sdk/usr/include/stdio.h:247:90: note: SDK / MacOSX10.13.sdk / usr / include / stdio.h:247:90:注意:
passing argument to parameter '__stream' here 在此处将参数传递给参数“ __stream”
...__ptr, size_t __size, size_t __nitems, FILE * __restrict __stream); ...__ ptr,size_t __size,size_t __nitems,FILE * __restrict __stream);
^ ^
1 warning generated. 产生1条警告。

here is my code: 这是我的代码:

#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;
}

I try to open a path or "random" file please help. 我尝试打开路径或“随机”文件,请帮忙。

According to manual page of fread() 根据fread()手册页

The function fread() reads nmemb elements of data, each size bytes long, from the stream pointed to by stream, storing them at the loca‐ tion given by ptr. 函数fread()从stream指向的流中读取每个长度为字节大小的nmemb数据元素,并将它们存储在ptr给定的位置。

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

Last argument of fread() is the FILE *stream that means file from where you want to read something and store into buffer . fread()最后一个参数是FILE *stream ,它表示要从中读取内容并将其存储到buffer

Replace 更换

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

with

fread(buffer,1,512,f1);

Also check the return value of fopen() 还要检查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;
}

your intention may be like this 你的意图可能是这样

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;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 警告:不兼容的指针类型将“char *”传递给“FILE *”类型的参数(又名“struct __sFILE *”) - warning: incompatible pointer types passing 'char *' to parameter of type 'FILE *' (aka 'struct __sFILE *') 不兼容的指针类型,将&#39;char *&#39;传递给&#39;FILE *&#39;类型的参数(又名&#39;struct__sFILE *&#39;) - incompatible pointer types passing 'char*' to parameter of type 'FILE*'(aka 'struct__sFILE*') 警告:从不兼容的指针类型返回 [-Wincompatible-pointer-types]| - warning: return from incompatible pointer type [-Wincompatible-pointer-types]| 来自不兼容的指针类型[-Wincompatible-pointer-types]的赋值 - assignment from incompatible pointer type [-Wincompatible-pointer-types] 来自不兼容指针类型的赋值[Wincompatible-pointer-types] - Assignment from incompatible pointer type[Wincompatible-pointer-types] 错误:- 从不兼容的指针类型 [-Wincompatible-pointer-types] 传递“get_string”的参数 1 - ERROR :- passing argument 1 of 'get_string' from incompatible pointer type [-Wincompatible-pointer-types] 警告:从不兼容的指针类型 [-Wincompatible-pointer-types] 传递“接受”的参数 2 - warning: passing argument 2 of ‘accept’ from incompatible pointer type [-Wincompatible-pointer-types] 从不兼容的指针类型 [-Wincompatible-pointer-types] 获取传递“pthread_create”参数 3 的警告 - Getting warning of passing argument 3 of ‘pthread_create’ from incompatible pointer type [-Wincompatible-pointer-types] 警告:从不兼容的指针类型 [-Wincompatible-pointer-types]| 传递 'transform_labels' 的参数 2 | - Warning: passing argument 2 of 'transform_labels' from incompatible pointer type [-Wincompatible-pointer-types]| 从不兼容的指针类型 [-Wincompatible-pointer-types] 传递“send_to_host”的参数 2 - passing argument 2 of ‘send_to_host’ from incompatible pointer type [-Wincompatible-pointer-types]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM