Wyo C++ Ch. 11 Notes
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 : audio cassette tapes
AS
random-access data files : audio compact discs (CD'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) //
same as if (outfile != 0)
{
outfile
<< "John Doe" << endl;
}
else
{
cout
<< "An error occurred while opening the file.\n";
}
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.
do
{
infile >> x;
if ( !infile.eof( ) )
{
cout << x << endl;
}
} while ( !infile.eof( ) );
Objective #8: Use multiple data files at the same time and insert data into the middle of a sequential access file.
Objective #9: Prompt the user for file names.
Objective #9: Pass file stream objects as parameters to functions.
int myFunc(istream &);
int main()
{
ifstream infile("junk.txt");
cout << myFunc(infile) << endl;
return 0;
}// end of main
int myFunc(istream &myfile)
{
int num = 0;
myfile >> num;
return num;
}
Note the use of istream (not ifstream) as the type used in the function's
header. Notice that the stream is passed by reference and the & operator
is used in the function header and prototype as well.