home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / CNEWS018.ZIP / BEGINNER.ZIP / GET_DATA.C < prev    next >
Text File  |  1989-11-14  |  1KB  |  37 lines

  1. /* GET_DATA.C */
  2. /* Read in 4 numbers from a file */
  3. /* Data passed to this function: none */
  4. /* Data returned from this function: The sum of 4 real numbers */
  5.   
  6. #include  <stdio.h>
  7.   
  8. float get_data(int *samp_size)
  9. {
  10.    FILE *in_file;                  /* declare an area for a file */
  11.    float sum_x = 0.0;            /* THIS WAS IN THE ABOVE CODE */
  12.    float in_val;                  /* declare an intermediate variable to */
  13.                                 /* be read in */
  14.    int i;                          /* a counter variable */
  15.   
  16.    if ((in_file = fopen ("mydata.dat", "r+t")) == NULL)
  17.    {
  18.       printf ("Oops, I don\'t see the data file\n");
  19.       exit(1);
  20.    }
  21.   
  22.    do {                       /* set up a loop to read in the file */
  23.   
  24.       fscanf (in_file, "%f", &in_val);   /* read the file */
  25.       sum_x += in_val;            /* add the value to the old sum */
  26.       (*samp_size)++;            /* increment the counter */
  27.        }  while (in_val >= 0);
  28.   
  29.    (*samp_size)--;            /* we have to decrement this because */
  30.    sum_x -= in_val;            /* tried to read in the stop number */
  31.                               /* and add in the stop number to the sum */
  32.   
  33.    fclose (in_file);            /* close the file */
  34.    return (sum_x);              /* return the sum to the main */
  35.                               /* program */
  36. }
  37.