Lab05, CISC181 honors, Spring 2004
Background
Your Deitel/Deitel text sometimes functions like a textbook, but other times, it functions more like a reference manual. For this lab, the "actual" reading
assignment is Chapter 6, about structs and classes. However, the following
sections may also be useful as "reference" material.
- Section 5.12 in Deitel/Deitel (C-style strings)
- Section 12.1 through 12.4 in Deitel/Deitel (Stream I/O with C-style strings,
using
cout and cin with C style strings, get,
getline, ignore, etc.)
Part 0: Copying files
Throughout this lab, you'l be using files from the lab06 subdirectory. You
might want to just copy all the files from that subdirectory into your current
directory now. Here's a command to do it:
cp -r ~pconrad/cisc181h/04S/labs/lab06 .
This will copy the entire lab05 subdirectory, along with all its
contents, into the current directory. You'll then have to cd into
lab05 to see all the files. The -r stands for recursive.
Part 1: Reading from a file until cin.eof()
- Copy the program
readUsernames.cpp from the lab05
subdirectory of the labs page on the course web site.
- Open up the file with a text editor, and notice the structure of the code.
What the program is doing is reading a series of usernames, and for each username
that it reads in, it writes out a URL (Uniform Resource Locator, that is,
a web address) that you can use to access that user's collegeDat.txt file.
However, rather than reading until we hit a "sentinel" value, or
until we've read a specific number of usernames, we use the cin.eof() function.
This function returns "true" when we have hit the "end of file"
on cin.
When running an interactive program (one where a human user is interacting
with the program, providing input and reading output), the user can signal
the end of file by typing "CTRL/D". So, essentially, this program
reads a line of input, and then repeateadly outputs URLs until the user puts
in CTRL/D.
- Compile and run this program, placing your output in the file readUsernames1
(rather than a.out). If you are not sure how to do that, consult your Anderson
book, the chapters on compiling C++ programs!).
- Try "pconrad" as the userid. Then, try your own userid, and those
of some of the classmates sitting near you in lab, or any other usernames
that you remember of CISC181 students. (Oh, you aren't working on this in
lab? Then make it a point to learn the usernames of some of your classmates
next time you come to lecture or lab!) When you've typed in a few userids,
hit CTRL/D to stop the program.
- You'll script this program as part of a later step, so you are done with
Part 1. Go on to part 2 now.
Part 2: Using redirection ( < ) to read standard input (cin) from a file
- Now, copy the file "usernames.txt" into your directory from the
lab05 subdirectory.
- Add your own username to this file, and also, add at least four additional
usernames of classmates of yours.
- Run the readUsernames program again, with the command line:
./readUsernames < usernames.txt
This will cause the program to take its standard input (cin) from usernames.txt
instead of from the keyboard. Notice that the output still contains all the
prompts for the input, which is a bit messy.
- Now, copy the program
makeHTML.cpp from the lab05 subdirectory.
Edit it, and look over the source code. In this program, I've taken out all
the prompts for input, and replaced all the output with "cout <<"
statements that will produce HTML code. The result is that when you run the
program with
./makeHTML < usernames.txt
you will get as output something that is suitable to be the HTML code for
a simple web page. You can store this into an HTML file via:
./makeHTML < usernames.txt > usernames.html
The "> usernames.html" part of the command line
is a shell feature that says "take the standard output (the cout stuff)
and put it into a file called usernames.html". Use "cat usernames.html"
to look at the contents of the usernames.html file.
- Now take your file "usernames.html", and copy it to a new subdirectory
under your public_html directory, called:
~userid/public_html/cisc181/files/lab05/usernames.html
and then browse it via http://copland.udel.edu/~userid/cisc181/files/lab05/usernames.html.
(Replace "userid" with your username (e.g. pconrad). You'll need
to do the "chmod"command, and use the proper "cp" command
before your file will be available on the web. You will also need to use an
"mkdir"command. . Check your old labs or your Anderson book for
information on how to do all that. Then, browse the page you created, and
check that the links work. If your own link doesn't work, then fix it! (This
is a chance for a do over of lab00.)
- Make a script lab05p2.txt in which you show the following steps:
- Compile readUsernames.
- Run readUsernames, inputting 3 usernames interactively, then typing
CTRL/D.
- cat your usernames.txt file to show that you added a few extra names
to it.
- Run readUsernames, redirecting the standard input from usernames.txt
(as shown above).
- Compile and run makeHTML, redirecting the standard input from usernames.txt,
and redirecting the output to usernames.html. (Note: you may have to delete
the old copy of usernames.html to make this work properly.)
Grading
- Parts 1 and 2 (lab05p2.txt): Each step in the list of
"what goes in the script" is worth 10 points, for a total of 50
points. In addition, having your files/lab00/collegeDat.txt and your files/lab05/usernames.html
files in the correct places on your web site is worth another 25 points each.
Total for this lab: 100 points.
Phillip T Conrad
Last modified: Thu Apr 1 10:11:26 EST 2004