/* * 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 "alphabet.h" bool processfile(char * filename, char * alphabet, int counters[]); int main(int argc, char * argv[]) { if (argc < 3) { printf("usage %s outfile infile(s)\n", argv[0]); return 1; } char alphabet[SCHAR_MAX+2]; build_alphabet(alphabet); printf("alphabet '%s'\n", alphabet); int num_counters = strlen(alphabet); int counters[num_counters]; for (int i = 0; i < num_counters; ++i) counters[i] = 0; int rval = 0; for (int i = 2; i < argc; ++i) { if (!processfile(argv[i], alphabet, 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, char * alphabet, int counters[]) { printf("processing input file %s\n", filename); /* * FIXME your code goes here * * outline: * * open file, printing an error message if open fails * 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%d%c" worked well * for me, and notice that sscanf returns a count similar to what * scanf returns), printing error messages for invalid lines * update counters */ return false; /* FIXME temporary so compiler doesn't complain */ }