
	/*
	** Parses an "all-meals.txt" file and prints out the largest meal
	** eaten each day between 1600 and 2100.
	*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])

{
FILE	*fpt;
int	i;
char	line[320];
int	subID,mealID,bites,duration;
double	kcals;
int	year,month,day,hour,minute;
int	max_bites,last_year,last_month,last_day,last_subID;

if (argc != 2)
  {
  printf("Usage: largest-meal-per-day [filename]\n");
  exit(0);
  }
if ((fpt=fopen(argv[1],"r")) == NULL)
  {
  printf("Unable to open %s for reading\n",argv[1]);
  exit(0);
  }
fgets(line,320,fpt); /* header line */
last_year=last_month=last_day=-1;
last_subID=-1;
max_bites=-1;
while (1)
  {
  i=fscanf(fpt,"%d %d %d %lf %d  %d %d %d %d %d",
	&subID,&mealID,&bites,&kcals,&duration,
	&year,&month,&day,&hour,&minute);
  if (i != 10)
    break;
  if (subID != last_subID  ||
	year != last_year  ||  month != last_month  ||  day != last_day)
    {
    if (last_subID != -1  &&  max_bites != -1)
      printf("%d   %d   %d %d %d\n",
		last_subID,max_bites,last_year,last_month,last_day);
    last_subID=subID;
    max_bites=-1;
    last_year=year;
    last_month=month;
    last_day=day;
    }
  if (hour < 16  ||  hour > 21)
    continue;
  if (bites > max_bites)
    {
    max_bites=bites;
    }
  }

}
