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.
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.
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:
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. Compile the file readUsersFromFile.cpp.
(Copied in step 1 of this lab).
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.
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 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.
lab06.cpp
(how to modify readUsersFromFile.cpp
)You will open an external file called usernames.html
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 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.)