CS 211 - Lab 2

Fall 2012

Due: Monday, September 10th, 2012

Head and Tail Commands

For this lab, you are to write two short C programs that will perform similar to the UNIX/Linux head and tail commands.

These commands display part of a file. The head command displays the first N lines of a file; while the tail command displays the last N lines of a file. The default value of N is 10.

The name of the file to be used is given via a command line argument.

Your program is to also to allow the user to display either more or less than 10 lines of the file. This will be done if the command line argument of -n is given. The command line "flag" (the term used to refer to command line arguments that start with a minus sign) of -n must be immediately followed by a positive whole number. This number is used to indicate how many lines should be displayed. The order of the filename and the -n flag can be given with either the filename first or the -n flag first.

Examples of Use

head myprogram.c
The first 10 lines of the file myprogram.c will be displayed.
tail myprogram.c
The last 10 lines of the file myprogram.c will be displayed.
head -n 15 myprogram.c
The first 15 lines of the file myprogram.c will be displayed.
head myprogram.c -n 7
The first 7 lines of the file myprogram.c will be displayed.
tail -n 21 myprogram.c
The last 21 lines of the file myprogram.c will be displayed.
tail myprogram.c -n 4
The last 4 lines of the file myprogram.c will be displayed.

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 -n is given
  3. a non-numeric value is given immediately after the -n flag
  4. a numeric value other than a positive integer is given. This value really should be a value of 1 or greater. It is silly to allow a command to display zero lines of the file
  5. no filename is given
  6. no value is given following the -n flag
These error message must be meaningful.

The C standard library <stdlib.h> has a nice little function called atoi() which stands for "Ascii to Integer" which converts a C-style string to an integer value. If the string does not contain a valid integer number, a value of zero is returned by the function.

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:

  • ptroy1Lab2head.c
  • ptroy1Lab2tail.c

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

  1. In the CS 211 Web Pages in Blackboard, go to the Assignments Page
  2. Click on the link "Lab 2". This will open a web page with the title: "Upload Assignment: Lab 2"
  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 2" page.
  7. You should see the Submission History page that shows what you submitted. Verify you actually submitted the correct information.
Topic revision: r1 - 2012-09-05 - 14:48:33 - Main.troy
 
Copyright 2016 The Board of Trustees
of the University of Illinois.webmaster@cs.uic.edu
WISEST
Helping Women Faculty Advance
Funded by NSF