c، رسالة التحذير التي تمرر الوسيطة 1 لـ "fstat" تجعل عددًا صحيحًا من المؤشر بدون تحويل

StackOverflow https://stackoverflow.com//questions/9709730

سؤال

سأعود إلى C بعد انقطاع طويل.إليك برنامجًا صغيرًا قمت بكتابته لإخراج حجم الملفات.إنه يجمع ويعمل بشكل صحيح، ويتم نسخه ولصقه إلى حد كبير من صفحة الدليل.لكنه يعطيني تحذيرا مزعجا من دول مجلس التعاون الخليجي.

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 *’`

أي تلميحات حول كيفية التخلص من هذه المشكلة (بدون تعطيل التحذيرات؛))

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

}
هل كانت مفيدة؟

المحلول

أنت بحاجه إلى تمرير واصف الملف, ، وليس أ FILE *.

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

جرب استخدام fileno(3) للحصول على واصف الملف من ملف FILE *.

int fd;

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

fstat(fd, &sb);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top