Lab06, CISC181, Fall 2004

Input files

The input files for this lab are in the lab06 directory in the usual place. 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.

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;

However, it is more interesting and challenging to write programs that process many hundreds, thousands, or millions of lines of input. When you write such programs, you really test whether your program "scales" or not... that is, is the solution that works well for 10 pieces of input is still a good solution for 10,000 pieces of input. An example of a well-known algorithm that doesn't scale well is bubblesort: it scales with n-squared, meaning that if you go from 10 pieces of input to 20,000 pieces of input, your program doesn't take about 2000 times longer; rather, it takes about 4,000,000 times longer (since 2000 squared is 4,000,000). So, we are going to learn how to read out input directly from a file on the disk.

It is possible to read input from a file through standard input (cin) by using Unix redirection, and a previous lab may have focused on that topic. In this lab, we are going to focus on reading input from a file directly.

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

  1. We don't have to worry about all the "useless prompts" that come up when we use redirection on a program that is designed to read input from the keyboard.
  2. 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.
  3. 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.
  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". You will open that file with a statement such as:

    ofstream htmlfile("usernames.html",ios::out);

    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 10 of your classmates (including yourself).

    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: 50 points total

Next Steps

There is a set of pre-lab exercises in the homework directory (hwk) called ple04.html. These are due at the beginning of lab next week!.