Lab 5 — Interpreting Command Line Arguments

Wednesday, April 15, 2009

Objective

·         To read and interpret arguments passed to a program on the command line.

Introduction

This week, you will write two short programs to use arguments on the command line. One program will add up a set of numbers on the command line and print the answer. The second program will copy one file to another.

You probably need to refer to your copy of Kernighan and Ritchie, especially section 5.10 (Command line arguments) and 7.5 (File access).

Getting Started

1.      Sign the attendance sheet.

2.      Create a directory to hold the files for Lab 5, and change to that directory now.

Part 1

3.      Write a program called Sum.c that comprises a single function that reads the arguments of the command line, interprets them as floating point numbers, adds them together, and prints the result. The function prototype for the function main() should be

int main(int argc, char *argv[]);    

The function should loop through each of the arguments (except argv[0]), convert its string to a double using atof, and add the result to a running total. When all arguments have been processed, it should print the sum on the standard output (i.e., the command shell window).

4.      Compile your program to the object file Sum and test it with the following command line:–

./Sum 1 2 3 4 5 6 7 8 9 10           

The result should be 55. If you have difficulty, use the debugger gdb to figure out the problem.

5.      Experiment with different numbers of and values of arguments to Sum, including the empty list of arguments, arguments that are floating point numbers, and arguments that are not numbers at all. See what happens.

Part 2

6.      Write another program to be called Copy.c that comprises a single function that reads two arguments from the command line, interprets them as file names, and copies one file to the other. Each file should be opened using the fopen() function described on page 160 of Kernighan and Ritchie. The first file should be the source of the copy, and it should be opened with the mode "r". The second file should be the destination of the copy, and it should be opened with mode "w".

Copy the file character-by-character, and count the characters that you copy. When the copy operation is complete, close both files and print the number of characters copied on the stderr.

Compile your program to the object file Copy.

7.      Test your program by copying the file Kennedy.txt. This can be found on the CCC systems at the path

/cs/cs2301-d09/Kennedy.txt

A command line for testing your program might be something like the following:–

./Copy /cs/cs2301-d09/Kennedy.txt myKennedy.txt

Open both the original and the copy in text edit windows and see if they look the same.

8.      Experiment with other files, including other txt files in the same directory, the program Sum that you created in step 4, and any others that you have time for. See if the copied files behave like the originals.

9.      Turn in your file using the turnin program. The Linux command you should use to submit your files is

/cs/bin/turnin submit cs2301 LAB5 Sum.c Copy.c

See you next week!