Exercise #5

Out: Wednesday, October 28, 2015
Due: Monday, November 2, 2015 by 11:15am.


We provide you with an implementation of wc (word count) that accepts a single file name as a command line argument and prints the number of lines, words, and characters in that file. You extend that code (in a very particular way described below) to allow more than one file name on the command line. The extended version prints the number of lines, words, and characters in all the files in total.

Our base code implements classes, to the extent that's possible in C. It follows the implementation shown in class, except that it separates the task of counting words into its own class, WordCounter, rather than embedding it in the mainline. The CharSource "class" is a base class specifying an abstract interface on which WordCounter relies - basically, it implements nextChar().

Your main job is to implement a new class, FileListReader, that similarly implements nextChar(), taking characters successively from a list of files. You also implement new mainline that can use both the FileReader and FileListReader classes. If only one command line argument is given, use an object of class FileReader to read it. If more than one is given, use an object of class FileListReader.

The existing code shows you how to create "a class". The most challenging part of this exercise is to understand the code that is already there. Once you do, implementing the new code is relatively easy, as it is very similar to the existing code.

You will have to:

Example results:
$ ./wc-list makefile
 15 lines, 48 words, 417 characters

$ ./wc-list makefile makefile 
 30 lines, 96 words, 834 characters

$ ./wc-list makefile makefile WordCounter.h
 56 lines, 168 words, 1560 characters

Your code must:

You should turn in your FileListReader.h, FileListReader.c, and main-wclist.c files to the course assignment dropbox.