-1

I want to calculate the sum and average on file.

for (i = 0; i > 50; i++) {
    fscanf(fp, "%d\n", &a);
    sum = a + sum;
    //printf("%d\n",a);
    count++;
}

but there is 50 as you can see that means I know how many integers that the file in it, I want to make a loop without that information. My loop should detect how many numbers in that file have.

As you can see I'm working on files.

1 Answers1

2

Read with fscanf until you reach EOF (end of file)

while (fscanf(fp, "%d\n", &a) != EOF) {
      sum+=a;
      count++;
}
Joonas P
  • 86
  • 1
  • 5