c程序,'fstat'的警告消息传递参数1从没有强制转换的指针生成整数

Bry*_*unt 4 c warnings pointers casting stat

经过长时间的休整,我又回到了c.这是我写的一个小程序来输出文件大小.它编译,并且它正常工作,并且几乎从手册页复制和粘贴.但它给了我一个恼人的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 *’`
Run Code Online (Sandbox Code Playgroud)

关于如何让它消失的任何提示(不禁用警告;))

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

}
Run Code Online (Sandbox Code Playgroud)

cni*_*tar 13

您需要传递文件描述符,而不是FILE *.

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

尝试使用fileno(3)从中获取文件描述符FILE *.

int fd;

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

fstat(fd, &sb);
Run Code Online (Sandbox Code Playgroud)