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 CS 537 - Programming Assignment #1
UNIVERSITY OF WISCONSIN-MADISON
Computer Sciences Department
CS 537
Spring 1996
Bart Miller
Programming Assignment #1
(Due Thursday, February 8, at 5pm)

I/O Processing in C++

There are several goals for this assignment. First, you will get a chance to familiarize yourself with the C++ programming language. Second, you will learn how to use some of the I/O facilities and library functions provided by UNIX and C++. Third, you will write code that will be useful for future assignments.

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 filename
Your program will open file filename and process it as described below in Program Details.

UNIX Manual Pages

You should become familiar with the UNIX manuals and the online "man" facility. This will be a great help in working on these assignments. For example, if you wanted to know how the fork (create process) system call works, you would type:
	man fork
The 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.

Program Details

Your program will first print out the name of the input file. Then it will open a file (perhaps using the ifstream constructor), read each line (using the getline method) and process them. For more information on C++ input/output, you can read the C++ book or type
	man iostream
	man fstream

Each line is of the form:

character-string  number1  number2
Each 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.

Command line parameters

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".

Deliverables

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.


Last modified: Fri Jan 26 11:26:21 CST 1996 by bart