【问题标题】:c program, warning message passing argument 1 of ‘fstat’ makes integer from pointer without a castc程序,警告消息传递'fstat'的参数1使指针从没有强制转换的整数
【发布时间】:2012-03-14 20:25:29
【问题描述】:

在长时间的中断之后,我又回到了 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 *’`

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

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

}

【问题讨论】:

  • 我在你的程序中只计算了 20 行,所以我看不到警告指的是哪一行 - 介意发布整个内容,也许在 [pastebin](www.pastebin.com) 上?
  • +1 使用“rb”作为图像文件

标签: c pointers casting warnings stat


【解决方案1】:

您需要pass a file descriptor,而不是FILE *

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

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

int fd;

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

fstat(fd, &sb);

【讨论】:

  • 伙计,那个错误信息完全是误导,我认为问题是由 &sb 引起的
  • 'fileno' 正是我所追求的。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多