I was given a problem to solve using fscanf which states the following:
Write a function to initialize array x of size SIZE with integers read from a text file. The function receives the name of the file as an argument.
The solution states the following below. Why would fscanf return a value < 1 if you're scanning each integer in the file? Shouldn't it be > 0 since fscanf returns the number of elements scanned?
void init (char *name)
{
FILE *fp;
int i;
fp = fopen (name, "r");
if (fp == NULL)
{
printf ("no file\n");
return;
}
for (i = 0; i < SIZE; i++)
{
if (fscanf (fp, "%d", &x[i]) < 1)
{
printf ("got %d numbers\n", i);
return;
}
}
fclose (fp);
return;
}