CISC181, Lab07, Fall 2005
Concepts covered in this lab:
- using ifstream to read an input file directly from disk
- reading until end of file (i.e. while (!infile.eof()), where infile is an ifstream object)
- generating HTML output files from within a C++ program
Using ifstream to open an input file
| Files you will copy from my web page: |
readUsersFromFile.cpp, usernames.txt |
| Files you should look at on my web page (for reference) but don't necessarily need to copy into your directory |
sample_usernames.html |
| Files you will create and submit via WebCT (also print ones in bold) |
lab07.cc, lab07.txt |
| Files you will place on your web page when finished under the URL http://udel.edu/~userid/cisc181/lab07 |
usernames.txt, usernames.html |
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. So, we are going to learn how to read out input directly from a file on the disk.
As a side note: 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).
Instructions:
- Copy the file
readUsersFromFile.cpp from the lab07 directory on my website into your directory.
- Look at the source code, especially the lines containing "ifstream" and "userInputFile". Here are some explanations of this code:
- "ifstream" is the name of a class for an "input file stream" object.
- "userInputFile" is an object of type "ifstream"; it can be used in place of "cin" as a stream to read data from.
- When we declare "userInputFile" as an object of type "ifstream", we invoke the constructor for the ifstream class, passing in the string "usernames.txt". (We'll talk more about constructors in our discussion of "Classes" in lecture).
- Invoking this constructor sets up the object "userInputFile" to refer to the file "usernames.txt".
- When you run this program, the code will look for a file named "usernames.txt" in your current directory on strauss. Operations such as
userInputFile >> x
will look in the file "usernames.txt" for input instead of looking for that input from the keyboard.
- Compile and 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.
- Copy the program to your own source code file called lab07.cc, 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 lab07 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 seven of your classmates (including yourself).
Make a script of this program called lab07.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.
In previous labs, I told you exactly what you needed to do in your script file. However, this week, you need to think about it yourself.
As you advance in your computer science classes, there will be less and less "spoon feeding" of exactly what needs to be done, and to a larger extent, you have to determine yourself what is necessary to "build the case" that your program operates correctly.
In real world software development environments, there are things called "code reviews", where you have to "make the case" to an audience of technical experts that your code is correct before it can become part of an officially released product. Preparing a script file is part of what you must do for a real world code review. In those cases, no-one is telling you step-by-step what needs to be done; on the other hand, if you leave out something important, you won't pass the review. So, we are challenging you to start developing those skills.
If you are not sure what 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
- Make sure you have a lab07.txt script file, per instructions. Upload to WebCT and print.
- Submit lab07.txt and lab07.cc to WebCT.
- Create a web directory ~/public_html/cisc181/lab07 and copy your usernames.txt and usernames.html file into that directory. Do NOT put your lab07.txt or lab07.cc 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
lab07.cc (50 pts)
- lab07.txt: 20 points for a complete script that shows all necessary steps and is correctly submitted per instructions
- ~/public_html/cisc181/lab07: 10 points for correct contents
- lab07.cc: 10 points for correctness, 10 points for programming style.
Total: 50 points