Image

(feof) problem

Hey, umm, I think I'm mis-understanding the action of the feof function in a program of mine. Isn't it supposed to check the input file to see if it's at the end? Mine doesn't appear to want to do that.
My input file is one.in and the program reading from it is one.c.
Would anybody care to lend me a hand?
Thanks,
-Phil


Whenever I run this program a message box tells me that it's impossible to divide by zero. My 'n' integer starts as zero but should be increasing as long as feof is false. Somehow the input file is being read as empty
/*
This program will compute and output the energy and mean values for
the signal data scanned from the input file.

Algorithm: -Follow a specific order of operations in a loop to accurately compute the two values:
1. fscanf(infile, "%f", &xval); <-- Scans a new x-value every time
2. if (feof(infile)) break; <-- Loop will end if there is no more input
3. n++ <-- Add 1 to the running 'n' integer
4. sume=sume+xval*xval <-- New summation value for the energey
summ=summ+xval <-- New summation value for the mean
-After the two summations have been tallied to the highest value of 'n', complete the
operation by multiplying each by (1/n)
-Output the two values
*/

#include
[Error: Irreparable invalid markup ('<stdio.h>') in entry. Owner must fix manually. Raw contents below.]

Hey, umm, I think I'm mis-understanding the action of the feof function in a program of mine. Isn't it supposed to check the input file to see if it's at the end? Mine doesn't appear to want to do that.
My input file is one.in and the program reading from it is one.c.
Would anybody care to lend me a hand?
Thanks,
-Phil

<lj-cut text="one.c">
<i>Whenever I run this program a message box tells me that it's impossible to divide by zero. My 'n' integer starts as zero but should be increasing as long as feof is false. Somehow the input file is being read as empty</i>
/*
This program will compute and output the energy and mean values for
the signal data scanned from the input file.

Algorithm: -Follow a specific order of operations in a loop to accurately compute the two values:
1. fscanf(infile, "%f", &xval); <-- Scans a new x-value every time
2. if (feof(infile)) break; <-- Loop will end if there is no more input
3. n++ <-- Add 1 to the running 'n' integer
4. sume=sume+xval*xval <-- New summation value for the energey
summ=summ+xval <-- New summation value for the mean
-After the two summations have been tallied to the highest value of 'n', complete the
operation by multiplying each by (1/n)
-Output the two values
*/

#include <stdio.h>
int main()
{
FILE* infile = fopen("one.in", "r");
FILE* outfile = fopen("one.out", "w");

float energy, mean, xval, sume=0.0, summ=0.0;
int n=0, i=1;

while(i==1)
{
fscanf(infile, "%f", &xval);
if (feof(infile))
break;
else
{
n=n+1;
sume=sume+xval*xval;
summ=summ+xval;
}
}
energy=((1/n)*sume);
mean=((1/n)*summ);
printf("\nThe energey in the signal was computed to be: %f\nThe mean value was computed to be: %f", energy, mean);
fprintf(outfile, "\nThe energey in the signal was computed to be: %f\nThe mean value was computed to be: %f", energy, mean);

return 0;
}

</lj-cut>
<lj-cut text="one.in">
-1.6279276e-03
-1.2096966e-03
-1.3479397e-03
-7.5253276e-04
-5.9086541e-04
</lj-cut>