I'm taking my first programming class ever this year and I'm slight confused on this. I'm trying to learn the fopen and fclose functions currently.
My code is
int main(void)
{
FILE *input; /* Pointer to the input file */
double values;
double sum;
input = fopen("data.dat", "r"); /* Prepare file for input */
sum = 0;
while (!feof(input))
{ fscanf(input, "%lf", &values);
sum = sum + values;
}
printf("The sum of the values is %f\n", sum);
fclose(input);
return 0;
}
When I compile this to get the sum, the last value of the data.dat file is counted for twice. I was wondering how I could fix this. Thanks!