How to fix warnings appeared while using fscanf()?

338 Views Asked by At

I'm using fscanf to read some int values from a file. It works correctly, but the way I did it, compiler gives a few warnings.

Source:

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

Warnings:

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] 

What am I doing wrong?

2

There are 2 best solutions below

0
On BEST ANSWER

redefine prototype of fscanf, actual code is different E.g fscanf("%d",&num). (before save?) – BLUEPIXY

0
On

You just need parenthesis

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