Please email me a brief synopsis (a few sentences will suffice) by Thursday, Nov. 8 describing your project and the people you will work with on it. In-class presentations will be scheduled beginning Nov. 27 (after Thanksgiving).
The objective today is to learn how parse an execute line to make a standalone utility. To illustrate, consider the sort utility. You can say
sort -n file.datwhich causes sort to read its data from file.dat (the explicit redirection < file.dat is not necessary). The command line also includes an option, -n, that asks sort to treat its input as numeric strings. The sort utility is quite flexible, insofar as if the file name argument is omitted, then it reads from the standard input.
The use of command line arguments is extremely useful, because programs that use them can be readily used in scripts and pipelines. A common but poor practice is for programs to prompt for options from the terminal. (Consider the problem of having to sort 100 different data files with a sorting program that, for each file, forces you to answer prompts regarding the name of the input file, whether the data is numeric, whether you want the data to be sorted in ascending order, etc.) A well-designed set of command-line options allows users to get the behavior that they desire for any number of files and makes your programs easy to automate with a script.
Unix/Linux systems include a utility function, called getopt, that simplifies the process of parsing command-line options. (The Posix shell also has a built-in function of the same name to allow command-line parsing in shell scripts.)
Practice exercise. Here is a sample C++ program that reads a command line for two possible options, a and n. The latter takes one integer argument. You can compile this program with
g++ -ansi -Wall -Wextra command_line.cc -o command_lineTry running it as
./command_line -a -n10and so on. See what happens if you invoke it with options other than a and n. Experiment and try adding some options. You do not need to turn in this program.
./command_line -an8 xyz.txt
Homework exercise. Write a C++ program that prints the mean, median, standard error and number of elements in the input. Your program should read from the standard input or from a file named on the command line, if present.
mean median standard-error n filenamewhere n is the number of elements in filename and standard-error is the usual estimator of the standard error of the mean.
stats -Mn filenameprints
median n filename
Put your source code into a file called stats.cc. Your program should handle the case of empty input gracefully (i.e., not crash). In addition, your program must not generate any diagnostics when compiled with
g++ -ansi -Wall -Wextra stats.cc -o stats(See the manual page for g++ for details on these options.)
See Koenig and Moo, p. 181, Section 10.5, for details and an illustration of the use of the ifstream facility.
You will create one file for this assignment, stats.cc. Since there is only one file, you may email it directly as an attachment to
mat420hw at gmail.comPlease include your name in the subject line! Assignments are due by November 13.
Copyright(c) 2007 by Eric J. Kostelich. All rights reserved.