We will not be studying the material on pp. 517-536 in this CMPSC 101 course.
Objective #1: Understand the uses for data files.
Objective #2: Understand the difference between sequential-access and random-access data files.
Sequential-access data files : video cassette tapes
AS
random-access data files : digital video discs (DVD's)
Objective #3: Open and close data files.
ofstream outfile;
Where ofstream is a C++ keyword indicating the type of pointer that you created and outfile is simply the identifier (i.e. variable name) for the file pointer that you want to declare. You could use any legal identifier name instead of outfile in this example.
The file pointer declaration statement above however only declares
the file pointer. It does not actually open a file from your computer's
hard drive. To actually open a file, you can use the statement
ofstream outfile("mydata.txt");
which declares outfile as an ofstream
file pointer object AND connects the file pointer to the actual file
mydata.txt that is saved in the same
folder as your C++ source file.
Note that you can use the open function
to make this connection between the file pointer and the actual file.
So the following two statements are actually equivalent to the last
statement above.
ofstream outfile;
outfile.open("mydata.txt");
and you can even specifiy the file stream mode to be output by using
the statement:
outfile.open("mydata.txt", ios::out);
but since the outfile pointer is
an ofstream object, C++ does not need to have the ios::out
argument in the open function call.
Having opened the mydata.txt file
with the outfile file pointer, your C++ program can now write store
data to that file. Storing data to a file is called "writing to
a file."
In order to be able to read data from a sequential-access file, you need to use an ifstream file pointer rather than an ofstream file pointer. The following examples could be used to to open a file for input rather than output. Note that we use the more logical file pointer name infile rather than outfile.
ifstream infile("mydata.txt");
or
ifstream infile;
infile.open("mydata.txt", ios::in);
but the last statement could be replaced by the simpler
infile.open("mydata.txt");
infile.close();
or perhaps
outfile.close();
Do not type the name of the file in the
parentheses.
Objective #4: Write to data files.
outfile << "John Doe";
to print that name to the next line in the data file pointed to by the file pointer, outfile.
It probably would be wise though to use the statement,
outfile
<< "John Doe" << endl;
or
outfile << "John Doe" << '\n';
so that a new-line character were placed there and the next piece of data to be written to the file would not butt up against the 'e' in "Doe".
It is also wise to first check to make sure that there wasn't an error actually opening the data file so one can use an if statement like the following to protect the program from crashing.
if (outfile.fail())
{
cout << "An error occurred while opening
the file." << endl;
exit(1);
}
Don't forget to include the cstdlib header file when you use the
exit function!
Objective #5: Read from data files.
infile
>> num;
instead of
cin >> num;
This statement stores the next numeric value
in the file pointed to by the file pointer infile
into the variable num.
Objective #6: Add to the end of a data file.
A file is treated as a stream in C++. Think of a file as information that either flows into your C++ program (i.e. reading from a file) or think of your program as sending a stream of information to a file (i.e. writing to a file.) There are three important stream operation modes (or stream access modes) to be aware of when working with files: input, output, and append. You have already been introduced to the input and output modes above.
ofstream appendFile;
appendFile.open("mydata.txt", ios::app);rather than
appendFile.open("mydata.txt");
or
appendFile.open("mydata.txt", ios::out);
Objective #7: Detect the end of a file.
while (!infile.eof())
{
infile >> x;
cout << x << endl;
}
Objective #8: Use multiple data files at the same time and insert data into the middle of a sequential access file.