Lab06, CISC181, Spring 2006

Introduction

Previous labs and projects have worked with ofstream objects to write data out to files.

In lecture we've talked about ifstream objects, but we haven't yet had any labs that deal with them. This week, we'll correct that omission with a simple exercise to read from an input file.

You are given some sample code that reads from an input file using an ifstream object, and writes data out to the screen. You'll modify the program to write that data out to a web file using an ofstream object.

Getting started—files and directories

The files for this lab are in the lab06 directory in the usual place—a lab06 subdirectory on the web site. See previous labs for examples of how to copy these files into your directory. And remember, of course, to make a new lab06 subdirectory before you get started.

Note that I'm not giving you specific instructions as to how to do that, because by now, you should be "catching on" to how Unix commands work. As you progress through the course, I'll be holding your hand less and less in the labs and projects, and expecting you to be more and more self-reliant. You'll find that is true in general as you progress through higher-level computer science courses.

Using ifstream to open an input file

We already know how to read input from the keyboard (standard input) using operations such as:

   cin >> x;

In this lab, we are going to focus on reading input directly from a file on the disk.

Reading directly from a file on the disk means has several advantages:

  1. You can open more than one input file at a time, and read different kinds of input from each one (e.g. one file might contain a "buddy list" for a chat program, while another contains a GIF or JPEG images that you want to use as your "icon" in that same chat program.
  2. You can still use cin to read other kinds of input (e.g. user commands that specify how to process the input in the file.) This part of the lab will show you an example of a program that does exactly that.

Step-by-Step Instructions

  1. Compile the file readUsersFromFile.cpp. (Copied in step 1 of this lab).

  2. Look at the source code, especially the lines containing ifstream and userInputFile. Here are some explanations of this code:

  3. Run the program. Note that the program's output goes to the screen, and consists of listing the usernames from the file, along with a count of how many usernames there were.

  4. Copy the program to your own source code file called lab06.cpp, and change the comments in the file accordingly. Modify the program so that in addition to the output now going to the screen, the program also writes out an external file called usernames.html. Detailed instructions follow in the next section.

Detailed Instructions for lab06.cpp (how to modify readUsersFromFile.cpp)

  1. You will open an external file called usernames.html with a statement such as:

        ofstream htmlfile("usernames.html",ios::out);
            
  2. Then write to that file the html code for a file containing usernames. An example of what that file should look like when you are done is in the lab06 subdirectory in the file sample_usernames.html. The file shows a web page with pointers to the cisc181 pages for each student in the input file. Modify the input file to contain the names of 5 of your classmates (including yourself). (Note that if you do this step in lab, you can just ask your fellow classmates what their usernames are.)

  3. Make a script of this program called lab06.txt that shows that the program works. Before you do your script, think about what needs to be in the script to show that the program works correctly. What files do you need to "cat" as part of your script to show the TA everything that is happening? Input files? Output files? Is a before and after directory listing needed? Should you delete any particular files before you run your program?

    You need to think this all through before you create your script file.

    If you are not sure steps to take, write down what you think needs to be done and show it to your TA, and ask him/her about it. Your TA should be willing to tell you if you are missing any important steps, and give you a hint about what those are. Your TA should also be willing to tell you if your outline is complete. However, it is not his/her job to tell you exactly what steps to do, and in what order. In this lab, it is part of your responsibility to figure that out.

Finishing up and Submitting

  1. Make sure you have a lab06.txt script file, per instructions. Upload to WebCT and print.
  2. Submit lab06.txt and lab06.cpp to WebCT.
  3. Create a web directory ~/public_html/cisc181/lab06 and copy your usernames.txt and usernames.html file into that directory. Do NOT put your lab06.txt or lab06.cpp file into that directory; you will lose points if you do. Only the .txt and .html files should be copied into that directory. Make the directory readable for all web users.

Grading: 100 points total

 


Valid XHTML 1.1 Valid CSS!