Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c program, warning message passing argument 1 of ‘fstat’ makes integer from pointer without a cast

I'm getting back into c after a long hiatus. Here's a little program I've written to output a files size. It compiles, and it works correctly, and it's pretty much copied and pasted from the man page. But it gives me an annoying warning from gcc.

gcc -ggdb  read_file_to_char_array.c -o read_file_to_char_array `mysql_config --cflags --libs && pkg-config --cflags --libs gtk+-2.0 && pkg-config --cflags --libs sdl`  
read_file_to_char_array.c: In function ‘main’:
read_file_to_char_array.c:22:19: warning:   [enabled by default]
/usr/include/i386-linux-gnu/sys/stat.h:216:12: note: expected ‘int’ but argument is of type ‘struct FILE *’`

Any hints as to how I can make it go away (without disabling warnings ;) )

#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>

int main(int argc, char **argv) {
    unsigned long *lengths;
    FILE *fp;
    struct stat sb;

    fp = fopen("image.png", "rb");
    fstat(fp,&sb);

    printf(" Size - %lld : ", (long long)sb.st_size);

    fclose(fp);

}
like image 640
Bryan Hunt Avatar asked Mar 14 '12 20:03

Bryan Hunt


1 Answers

You need to pass a file descriptor, not a FILE *.

int fstat(int fildes, struct stat *buf);

Try using fileno(3) to get the file descriptor from a FILE *.

int fd;

fp = fopen("image.png", "rb");
fd = fileno(fp);

fstat(fd, &sb);
like image 55
cnicutar Avatar answered Sep 28 '22 16:09

cnicutar