0
votes

how to fix?

warning:

incompatible pointer
types passing 'char [16]' to parameter of type 'FILE *'
(aka 'struct __sFILE *') [-Wincompatible-pointer-types]
fread(buffer,1,512,data);
^~~~
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/
SDKs/MacOSX10.13.sdk/usr/include/stdio.h:247:90: note:
passing argument to parameter '__stream' here
...__ptr, size_t __size, size_t __nitems, FILE * __restrict __stream);
^
1 warning generated.

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.

1
The definition of fread expects the last argument to be of type FILE*, so why not pass it f1?Neijwiert
You pass the FILE pointer fl correct to fgets, but not when you call fread. Why? Is it just a typo?Some programmer dude
fread(buffer,1,512,data);-> fread(buffer,1,512,f1);. And also you should check the return value of fopen, if it's NULL -> file coulnd not be opened.Jabberwocky
data is uninitialized, which means it contains unspecified values, which means fopen(data,"r"); will likely overrun buffer, which is undefined behaviour. You also don't check if fopen succeeded. If it did not, then using it on fgets results in undefined behaviour.user694733
fread(buffer,1,512,data); Besides your problem with last parameter, you have UB here. You read 512 bytes of data into a buffer of 8 bytes.Gerhardh

1 Answers

3
votes

According to manual page of 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.

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.

Replace

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

with

fread(buffer,1,512,f1);

Also check the return value of 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;
    }