如何修复使用 fscanf() 时出现的警告?

How to fix warnings appeared while using fscanf()?

我正在使用 fscanf 从文件中读取一些 int 值。它工作正常,但我这样做的方式,编译器给出了一些警告。

来源:

FILE *fp = NULL;
fp = fopen(argv[1],"r");
int num;
while(fscanf(fp,"%d",&num) != EOF) // This is the line 46
    printf("%d ",num);

警告:

passing argument 1 of ‘fscanf’ from incompatible pointer type
format not a string literal and no format arguments [-Wformat-security]
passing argument 2 of ‘fscanf’ from incompatible pointer type
Line breakpoint: main.c [line: 46] 

我做错了什么?

你只需要括号

while( (fscanf(fp,"%d",&num)) != EOF) // This is the line 46

重新定义fscanf的原型,实际代码不同例如fscanf("%d",&num)。 (保存之前?)– BLUEPIXY