Date: Mon, 11 Nov 1996 17:24:54 GMT Server: NCSA/1.5 Content-type: text/html Last-modified: Mon, 29 Jan 1996 03:03:41 GMT Content-length: 4466
UNIVERSITY OF WISCONSIN-MADISON
Computer Sciences Department | ||
CS 537
Spring 1996 | Bart Miller | |
Programming Assignment #1
(Due Thursday, February 8, at 5pm) |
Your assignment is to write a program that takes an input file, reads through it line by line, do some simple processing on each line, and then produces some output. Your program, to be called writer, will be invoked:
writer filenameYour program will open file filename and process it as described below in Program Details.
man forkThe UNIX manual is organized into many sections. You will be mainly interested in the first three sections. Section 1 is for commands, like ls, g++, or cc. Section 2 is for UNIX system calls (calls directly to the UNIX kernel), such as fork, open, or read. You will typically not use Section 2. Most of what you use will be in the UNIX library routines in Section 3. These are calls such as atof, or strcpy. More details about the online manual will be given in Discussion Sections.
man iostream man fstream
Each line is of the form:
character-string number1 number2Each of these three pieces will be separated by any number of blank characters (spaces or tabs). The numbers can have decimal points and fractional parts. For each input line, you will print to standard output (using cout <<) a line containing the character-string, the two numbers, and the sum of the two numbers.
The program will continue until the end of the input file.
UNIX supplies several library routines to make this assignment easier. For example, to convert a ASCII string to a number, you can use atoi or atof (pronounced, "A to I", ASCII to integer, and "A to F", ASCII to float). Use the "man" command to look up the details. To copy strings, you can use strcpy; to compare strings, you can use strcmp. Do a man string to look up these routines.
UNIX and C++ make command line parameters easily available. The main procedure in your program should look like:
#include <iostream.h> #include <fstream.h> int main (int argc, char *argv[]) { cout << "Program " << argv[0] << " first arg=" << argv[1]; . . . }
The command line is processed (by the shell) into a list of character strings, one for each argument (including the command name). You can reference the integer argc to tell you the number of arguments. The array argv is the list of strings. So, if you typed in the command appearing at the beginning of this assignment, argc would contain the value 2, argv[0] would contain the string "writer", and argv[1] would contain the string "filename".
You should hand in a print-out of your program and output from your program on the sample files that we will provide. We will announce in class where to find these files.