CMPSC 201 - Ch. 8 & Ch. 7.2 Notes
Note that we will not be studying sections 8.3 through 8.6 in this class.
Objective #1: Explain the concept of file stream objects in C++.
- Often a C++ program will use an external data file to receive its
input and possibly another file to store its output.
- While it may be convenient to ask the user to enter data via the keyboard,
it is more practical to obtain large amounts of input from a previously saved
external data file.
- While it may be useful to store output to the screen so that a user can
see a program's results, it is also useful to store the output to an external
- You must use a file stream object (aka file pointer) for each external
file that you plan to work with in your C++ program.
- A file stream itself is simply a path from your program to a physical
device such as a floppy disk or the hard disk. Every file stream has a mode
which determines which direction the information will flow. Either the information
flows from the floppy or hard disk TO the C++ program or vice versa. If the
information flows into the C++ program, we call the mode input and
the process of getting information from the floppy or hard disk is called
"reading". Otherwise, the mode is called output and we call
the act of storing information on the floppy or hard disk "writing".
By the way, there is no mode that allows information to flow in both directions
to the same file stream object.
- To create a file stream object that would be used to read a value from an
external data file, this statement might be used:
where ifstream is a C++ keyword and the identifier infile was chosen by the
programmer. The programmer could use any valid identifier (i.e. variable name)
in place of infile.
- To create a file stream object that would be used to write data to an external
data file, this statement might be used:
where ofstream is a keyword and outfile was chosen by the programmer.
- Usually these statements are placed at the top of the function (i.e. main)
in which the file stream objects and external files are going to be used.
These statements are somewhat similar to a variable's declaration statement
that should also be located at the top of a program.
- When using file stream objects, you must use include the header file fstream.h
at the top of your program with the compiler directive,
Objective #2: Use the common file stream methods including open and close.
- After a file stream object is created, you must actually open the named
file that exists on the floppy or hard disk. The following statement would
be used to actually open a file named "HighScores.txt" and to connect
it to the file stream object infile that was instantiated (i.e. declared)
with the line above.
- After you are completely finished using an external data file, you should
close the file with a statement like,
- You can have multiple files opened for both reading and writing at any point
during a C++ program however you must have separate file stream objects connected
with each one.
Objective #3: Read from and write to sequential files using the necessary
file stream methods.
- You can use the extraction operator ( << ) with a file stream object
to read from a file that is opened with the input mode, just as you are can
use the extraction operator (aka input operator) with the cin object.
- While the statement,
cin >> age;
would require the user to input his/her age via the keyboard, the statement
infile >> age;
would read the first line of the external text file that is connected to the
file stream object infile. The statement,
can be used to read the whole first line of the file and store that line in
the character array sentence.
- To write to a file that is opened for output, you use the insertion operator
<< (aka output operator) with its file stream object.
- While the statement,
cout << favoriteNum << endl;
would display the value of favorite_num to the screen, the statement
outfile << favoriteNum << endl;
would write it on a single line of the file connected with the file stream
- Use the eof function along with a do while loop to determine
whether the end of a sequential-access file has been reached. This function
returns a 1 (true) if an attempt has been made to read past the end of the
file. For example,
infile >> x;
if ( !infile.eof( ) )
x << endl;
} while ( !infile.eof( ) );
- For one C++ program to access multiple data files, you
simply have to create multiple file stream objects (each one pointing to a
different data file). For example,
uses two file stream objects, infile (an ifstream file object) and outfile
(an ofstream file object). infile points to a external data file named inputfile.txt,
which contains data that will be read into the C++ program. outfile points
to the external data file named outputfile.txt, which will be written to by
the C++ program.
Objective #4: Use string variables. (See Ch. 7.2 in your textbook for this discussion of the use of strings. This is the only part of Ch. 7 that we are studying in this class.)
- The simple C++ data type char can only store one character at a time. In
order to store a string such as "John Doe", you can use a string
- You can change one character of a string using subscript notation. For example,
string studentName = "Jon";
studentName = 'D';
studentName is now "Don" not "Jon" since the first character
of a string is considered to be stored in index position 0. You will see the
use of square brackets when we learn about arrays in this course.
- You must declare a string variable before you use it in a program like any
other variable. To declare a string variable, use a statement like,
- To declare and initialize a string, use the following:
string name = "John Doe";
- To obtain the user's name as input you can use the cin object like this,
cout << "Enter your first name: ";
cin >> firstName;
cout << "Enter your last name: ";
cin >> lastName;
where first_name and last_name are two separate character arrays. The user
would have to press the Enter key however after each name. Notice the use
of the extraction operator (aka input operator) >> after the
keyword cin and the use of the insertion operator (aka output operator)
<< after the keyword cout. Then, you could printout the person's name
cout << firstName << " " << lastName <<
- You must include the string header file at the top of programs in which
you wish to declare or use string variables.
using namespace std;
string name = "John Doe";
cout << name << endl;
- However, when opening a file with a user-specified filename as in
Program 8.3a on p. 453, you will have to use the following syntax (notably
the c_str function)
using namespace std;
cin >> filename;
outfile << "hello" <<
A string literal is a sequence of characters that is used in a C++
program between double quotes such as in the statement
cout << "Hello world!";
where "Hello world!" is the string literal. Note that the string
literal, in this case, includes a space and an exclamation point. You do
not have to include the string header file in a program when you use string
A character literal is a single character that is used in a C++
program between single quotes. In the statement,
initial = 'T';
the character literal is the
uppercase letter T.