CS 211 - Lab 2

Fall 2012

Due: Monday, September 17th, 2012at 11:59 pm

Word Count: The wc Command

For this lab, you are to write a C program that will perform similar to the UNIX/Linux wc command.

The wc command can give information on the total number of

  • characters/bytes in a file (using the -c flag)
  • words in a file (using the -w flag)
  • lines in a file (using the -l flag - lower case L)
  • the number of characters in the longest line in a file (using the -L flag - upper case L)
The command syntax is to first list any command line flags then list one or more file names.

If no command line flags are given, give the same output as if the -c, -w and -l flags were given.

Note that the flags can be given in any order; however, the output will

  1. first show the line count,
  2. then the word count,
  3. then character/byte count,
  4. then the "max line length",
  5. the final output item will be the name of the file.
Of course, output is shown only if the corresponding command line flag is given!

If multiple files are given, a separate output line is given for each file. Also an additional line is given to show the totals of the statistics requested. Note that the max-line-length "total" is actually the maximum max-line-length and NOT the total of the max-line-lengths. These statistics are to be given in right justified columns. Use of the printf() function will greatly help with this. Note that in the examples, this formatting keeps getting removed by the wiki.

A word is a non-zero sequence of "non-white-space" characters delimited by "white-space" characters. A "white-space" character is any character that returns true when given to the isspace() function. These characters are normal the following:

  • space
  • horizontal tab - \t
  • vertical tab - \v
  • newline - \n
  • form feed - \f
  • carriage return - \r

Examples of Use

wc fcopy.c
40 125 820 fcopy.c
wc -l -w -c -L fcopy.c
40 125 820 77 fcopy.c
wc -L -l fcopy.c
40 77 fcopy.c
wc -l -c -w -L fcopy.c head.c a.exe
40 125 820 77 fcopy.c
39 112 766 77 head.c
184 1171 46260 2508 a.exe
263 1408 47846 2508 total
wc -w -l fcopy.c head.c
40 125 fcopy.c
39 112 head.c
79 237 total
wc fcopy.c head.c
40 125 820 fcopy.c
39 112 766 head.c
79 237 1586 total

If invalid command line arguments are given, your program is to detect this error, display an error message and end execution. Possible invalid arguments would include

  1. a non-existant filename is given
  2. a flag other than those defined is given
  3. no filename is given
These error message must be meaningful.

To determine the number of lines in a file, you may need to read the file character by character counting the number of newline characters you encounter. The newline character is represented by the special character sequence of '\n' (backslash n).

Program Submission

Your are to submit the programs for this lab via the Assignments Page in Blackboard.

To help the TA, name your file with your net-id and the assignment name, like:

  • ptroy1Lab3.c

Submit this file via the Assignment Link for the Lab in Blackboard.

  1. In the CS 211 Web Pages in Blackboard, go to the Assignments Page
  2. Click on the link for the correct lab. This will open a web page with the title: "Upload Assignment: Lab X", where X is the number of the lab
  3. Scroll down and click on the button "Browse for Local File"
  4. Select the file that you created that contains the program. Then click OK.
  5. Repeat steps 3 and 4 for your second program.
  6. Click the submit button on the "Upload Assignment: Lab X" page.
  7. You should see the Submission History page that shows what you submitted. Verify you actually submitted the correct information.
-- Main.troy - 2012-09-12
Topic revision: r2 - 2012-09-12 - 16:15:13 - Main.troy
Copyright 2016 The Board of Trustees
of the University of Illinois.webmaster@cs.uic.edu
Helping Women Faculty Advance
Funded by NSF