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