Code:
#include <stdio.h>
#include <stdlib.h>
FILE *output ;
FILE *input ;
struct sale
{
int week ;
char name[30] ;
int units ;
int price ;
int total ;
} ;
void result(int week[])
{
for(int i = 0 ; i < 13 ; i++)
{
fprintf(output, "\n%i %g", i, ((float)week[i]/100)) ;
}
}
void close(void)
{
fclose(input) ;
fclose(output) ;
}
int main(int argc, char *argv[])
{
int week[13] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} ;
struct sale info ;
struct sale largest ;
info.units = 0 ;
info.price = 0 ;
largest.price = 0 ;
largest.units = 0 ;
input = fopen(argv[1], "r") ;
output = fopen(argv[2], "w") ;
if(( fopen(argv[1], "r")) == NULL)
{
printf("Sorry, this file cannot be opened.\n") ;
exit(1) ;
}
else if(( fopen(argv[2], "w")) == NULL)
{
printf("Sorry, this file cannot be opened.\n") ;
exit(2) ;
}
while(fscanf(input, "%i%30s%i%i", &info.week, info.name, &info.units,
&info.price) != EOF)
{
week[info.week] += info.units * info.price ;
info.total = week[info.week] ;
if(info.price * info.units > largest.units * largest.price)
{
largest.price = info.price ;
largest.units = info.units ;
largest.week = info.week ;
info.name[info.week] = largest.name[1] ;
}
}
fprintf(output, "WEEK TOTAL SALES") ;
result(week) ;
fprintf(output, "\nThe largest single sale was in week %i, when %i units of %30s were sold at %g per unit.\n", largest.week, largest.units,
largest.name, (float)largest.price/100) ;
close() ;
}