Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I could use some help. Can someone please tell me why my code gives the following error when compiled:
reverse.c:17:warning: passing arg1 of 'fscanf' from incompatible pointer type
reverse.c.17:warning: passing arg2 of 'fscanf' from incompatible pointer type#include <stdio.h>
int main () {
FILE *f;
int count, i, tmp, *v;/* open the file called "numbers" for reading */
f = fopen ("numbers", "r");
if (!f) {
fprintf (stderr, "hey!\n");
exit (1);
}/* count the number of items in the file */
for (count=0; !feof (f); count++) fscanf ("%d", &tmp);
count--;/* close the file */
fclose (f);
/* allocate space for an array called 'v' */
v = (int *) malloc (sizeof (int) * count);
/* open the file again for reading */
f = fopen ("numbers", "r");
/* read all the numbers into 'v' */
for (i=0; i<count; i++) fscanf (f, "%d", &v[i]);
/* close the file */
fclose (f);
/* print 'v' from last element to first element */
for (i=count-1; i>=0; i--) printf ("%d\n", v[i]);
exit (0);
}
I would appreciate your comments and suggestions. Thank you.

Try making this change:
Yours: for (count=0; !feof (f); count++) fscanf ("%d", &tmp);
Mine: for (count=0; !feof (f); count++) fscanf (f,"%d", &tmp);
If you read up on fscanf, you will see that it expects the stream to be included. It is kind of hard to read the file if you don't tell the function where it is.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |