home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 27 / CDROM27.iso / linux / lg / issue32 / rogers_example3c.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-30  |  1.1 KB  |  49 lines

  1. #include <stdio.h>
  2.  
  3. /*
  4.    this program reads in a data file consisting of
  5.    multiple lines, each line contains a floating 
  6.    point number and a single character label.
  7.    This simulates multiple accounts with charges
  8.    and credits.  The program will add up all charges
  9.    and credits and print a total for each.
  10.  */
  11.  
  12. main ()
  13. {
  14.  
  15.   float value, add[255];
  16.   char label[1024];
  17.   int i;
  18.   FILE *stream;
  19.  
  20.   /* initialize the array to zero */
  21.   for (i = 0; i < 255; i++)
  22.     add[i] = 0;
  23.  
  24.   /* open the input file for reading, quit if it doesn't open */
  25.   if ((stream = fopen ("example3.dat", "r")) == (FILE *) 0)
  26.     {
  27.       fprintf (stderr, "Couldn't open example3.dat file.");
  28.       return 1;
  29.     }                /* end if */
  30.  
  31.   /* read from the file until you reach the end of the file */
  32.   while (fscanf (stream, "%e%s", &value, label) != EOF)
  33.     {
  34.       add[*label] = add[*label] + value;
  35.     }                /* end while */
  36.  
  37.  
  38.   /* print out the totals for the files by label */
  39.   for (i = 0; i < 255; i++)
  40.     {
  41.       if (add[i] != 0)
  42.     printf ("%c    %6.2f\n", i, add[i]);
  43.     }
  44.  
  45.   /* we are done and successful */
  46.   return 0;
  47.  
  48. }                /* end main */
  49.