Word Count Programs

The goals of this lab are threefold: to learn how to read and write a text file, to learn to handle streams and to use command line argument. First copy the files directory by typing
   cp -rd ~mc38/labs/word-count/files /wordcount

Count characters, words and lines

The goal of this part of the lab is to write a program similar to the program described in programming exercise P10.1 on page 472 of the text.
  1. Got to any directory that has a text file in your account and type
         wc filename
    
    The program wc outputs four fields, namely If one specify more than one file, it will also calculate the total of the three number fields and outputs it.

  2. In the file wordcount.cc write three procedures countChars(char*), countWords(char*) countLines(char*) that takes the filename of a file as the parameter and returns repsectively the number of chars,the number of words and the number of lines.

  3. Your task is to write a program that mimicks the wc Unix commands, i.e., wordcount will take command line arguments and process the file(s) and prints the number of characters, words and lines in that file in that order. For the command line argument to work, it will not be enough to start with int main() instead it will have to have arguments in the main function.

  4. To process all the files, you might need a loop that goes something like
         for (int i = 0; i < argc; i++){
          	 ...
    	 ...
    	 }
    

  5. To make sure your program runs properly, you may test it with
       wordcount story1.txt story2.txt story3.txt story4.txt
    
    and if you did not change the original story files a correct output will be
      3853    650   74  story1.txt
      4500    774  102  story2.txt
      6825   1159  139  story3.txt
      9561   1657  191  story4.txt
    

  6. Check-off: Make sure you have clean codes and the program is designed and structured so it is easy to read and maintained by other programmers.

  7. Optional : The way the program was written, we need to open the file three times which is terribly inefficient considering how slow each file I/O is. The way around it is to use only one procedure to calculate numOfChars, numOfWords and numOfLines. Write a procedure count(int&, int&, int&, string fileName) that takes the fileName, reads the file only once and returns the required character, word and line counts as reference parameters. You may need to convert a string to a stream using istringstream.

  8. Optional Challenge : Write the program so that if there are multiply files, it also prints the total character, word and line counts of all the files.

Wildcards on Unix : Do you know that your successful program also accepts the wildcards "*" and "?" (for instance, instead of listing story1.txt to story4.txt, we can issue the command

    wordcount story?.txt
or
    wordcount story*