CPS120: Introduction to Computer Science

Download Report

Transcript CPS120: Introduction to Computer Science

CPS120: Introduction to
Computer Science
Data Files
Working with Data Files
• It is possible to have a C++ program read
and use a data file that is stored on your
PC's hard drive
– Rarely does a program work without relatively
large amounts of data.
– Rather than requiring the user to input large
amounts of data, one often "reads" data from an
external data file.
Using External Files
• External data files are simply text files
(usually with the extension .txt or .dat) that
are stored in the same folder as the C++
program which access them.
• It is possible to have a C++ program read
files that are stored anywhere else on your
PC's hard drive or network
– You will have to supply the path of those files
in order to successfully access them.
Sequential Files
• Stores data as one long continuous piece of
data
• To access a specific record (piece of data),
though, the C++ program must read through
all of the former pieces of data
Opening and Closing Data Files
• Files must be officially be opened before
they are used by a C++ program
Required Compiler Directives
• Any program that uses file pointers must
include the fstream.h header file with the
compiler directive,
#include <fstream.h>
at the top of the program
Preparing to Use Files
• Opening a sequential-access file
ofstream outfile;
– ofstream is a C++ keyword indicating the type of
pointer that you created
– outfile is simply the programmer's chosen name
for the file pointer (and can be any valid name)
• Open the file "mydata.txt" that is stored on
your PC's hard drive
outfile.open("mydata.txt", ios::out);
or the shorter version
outfile.open("mydata.txt");
– output (out) is the default type of access for ofstream
objects
Reading From a File
• Declare a file pointer as an ifstream object
with:
ifstream infile;
– ifstream is a keyword and infile is the name for
the file pointer.
• Open the actual file for reading with:
infile.open("mydata.txt", ios::in);
or the shorter version
infile.open("mydata.txt");
Using Sequential Files
• A sequential access file cannot be
opened for input and output at the same
time
• You can close a file that was opened for
input and reopen it for output during a
program's execution
Closing Files
• It is very important to always officially
close a file, when you are finished using it
from within a C++ program.
• The following statement would close the
file, which is pointed to by the file pointer
infile
infile.close();
Preparing to Write Output
• It is wise to check to make sure that there
wasn't an error actually opening the data file
• One can use an if statement like the following
to protect the program from crashing.
if (outfile) // same as
if (outfile != 0)
{
outfile << "John Doe" << endl;
}
else
{
cout << "An error occurred while opening the
file.\n";
}
Writing Output
• To write data to a sequential-access data file
you would use a statement like:
outfile << "John Doe" << endl;
to print that name to the next line in the data
file pointed to by the file pointer, outfile.
Reading Data From Files
• Use the insertion operator (>>). But, instead
of using the cin keyword, you use the file
pointer name that you declared.
infile >> x;
• This statement stores the next numeric
value in the file pointed to by the file
pointer, infile, into the variable, x.
Appending Data
• Adding data to the end of a sequential-access data
file is called appending
• Open the file using the ios::app stream operation
mode as in:
outfile.open("myfile.txt", ios::app);
• where the app is short for append.
• If you accidentally open the file with the ios::out
mode, you will end up overwriting data in the file
because C++ will write the first piece of outputted
data at the beginning of the sequential-access data
file
Detecting the End of a File.
• Use the eof function 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.
do
{
infile >> x;
if ( !infile.eof( ) )
{
cout << x << endl;
}
} while ( !infile.eof( ) );
Using Multiple Data Files
• For one C++ program to access multiple
data files, you simply have to create
multiple file pointers (each one pointing to a
different data file). For example,
infile.open("inputfile.txt");
outfile.open("outputfile.txt");