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