/* Numread.c */
#include<stdio.h>

int main()
{
 float x, sum;
 int count;
 FILE *infile;    /* declares file pointer named infile */

 infile = fopen("floats.dat", "r");  /* open file for input */

 sum = 0.0;  /* initialize sum */
 count = 0;  /* initialize count */
 if (infile)  /* If no error occurred while opening file */
  {           /* input the data from the file. */
   printf("The numbers in the data file are as follows:\n");

   do  /* read numbers until 0.0 is encountered */
    {
      fscanf(infile, "%f", &x);        /* get number from file */
      printf("%.1f\n", x);  /* print number to screen */
      sum = sum + x;      /* add number to sum */
      count++;            /* increment count of how many numbers read */
    } while(x != 0.0);
   /* Output sum and average. */
   printf("The sum of the numbers is %.1f.\n", sum);
   printf("The average of the numbers (excluding zero) is %.1f.\n",
	   sum / (count - 1));
  }
 else          /* If error occurred, display message. */
  {
   printf("An error occurred while opening the file.\n");
  }
 fclose(infile);  /* close the output file */
 return 0;
}
