Data Files

advertisement
CPS120: Introduction to
Computer Science
Lecture 15 B
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
• Each different element is stored next to the prior
piece of element
• There are no "empty bytes" of disk space in
between consecutive pieces of data
• To access a specific record (piece of data), though,
the C++ program must read through all of the
former pieces of data
Random-Access Files
• A random-access external data file stores
data into blocks of memory of equal size
• This may waste some disk space, it does
speed up access times.
An Analogy
Sequential-access data files compare to
audio cassette tapes
AS
random-access data files compare to
audio compact discs (CD's)
Opening and Closing Data Files
• Files must be officially be opened before
they are used by a C++ program
• To open a sequential-access file, you must
first declare a file pointer that you intend to
point to the file that you will open
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 cn 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");
Sequential Read / Write
• One disadvantage to using sequential-access files
is that it is tedious to insert data in the middle
• The common method is to open the file and read
each line, one at a time.
– As you read each line, you write the information into a
new, blank file that is opened for output.
– When you reach the point at which you wish to insert
new information, you simply write that information to
its proper place in the new, second file.
– Then, continue to read each line from the first file and
copy it to the second file
Prompting for File Names
• Using the c_str function with a string variable, you can ask
the user to input the file to be opened and store that
filename in a string variable. Then, you can open the file.
For example,
string fileName;
ifstream infile;
cin >> fileName;
infile.open(filename.c_str());
// user's input
// the file for input
// getting user's inputted filename
// opening the file for input
Standardized File Handling
• In order to standardize file handling, many
modern operating systems include their own
set of file handling functions that
programmers can use.
– Microsoft Windows programmers can use
predefined funtions from the Microsoft
Foundation Class library (MFC library), a C++
implementation of the Windows application
programming interface (API).
Download