/* * Program to merge counts created by countalpha program. * * Names of output file and one or more input files are given as command-line * arguments. * * Program merges counts and writes the result to output file. * * Programs also prints some tracking output (names of input files, error * messages for any input files that can't be opened, error messages if * there's anything in an input file that's not in the format produced by * "countalpha". */ #include #include #include #include #include "alphacounters.h" bool processfile(char * filename, letter_count_t * counters, int num_counters); int main(int argc, char * argv[]) { if (argc < 3) { printf("usage %s outfile infile(s)\n", argv[0]); return 1; } int num_counters; letter_count_t * counters = build_alphacounters(&num_counters); if (counters == NULL) { printf("could not get space for %d counters\n", num_counters); return 1; } print_alphabet(stdout, counters, num_counters); int rval = 0; for (int i = 2; i < argc; ++i) { if (!processfile(argv[i], counters, num_counters)) rval = 1; } /* FIXME your code to write to output file goes here */ return rval; } /* * function to process one input file * * returns true if file could be opened and contains all good data, * false if the file could not be opened or contained at least one * line that's not in the right format. * if file could be opened, increments counters. */ bool processfile(char * filename, letter_count_t * counters, int num_counters) { printf("processing input file %s\n", filename); /* * FIXME your code goes here * * outline: * * open file, printing an error message if open fails and returning * false * read the file a line at a time with fgets (it returns NULL at * end of file) * print a message for any lines that are too long * parse lines with sscanf (format string "%c%c%ld%c" worked well * for me, and notice that sscanf returns a count similar to what * scanf returns), printing error messages for invalid lines * for each line, update counter using update_counter() function */ return true; }