|
|
|
|
|
|
|
executes the then-clause if the last I/O operation on inFile succeeded. The statement |
|
|
|
|
|
|
|
|
executes the then-clause if inFile is in the fail state. (And remember that once a stream is in the fail state, it remains so. Any further I/O operations on that stream are null operations.) |
|
|
|
|
|
|
|
|
Here's an example that shows how to check whether an input file was opened successfully: |
|
|
|
|
|
|
|
|
#include <iostream.h>
#include <fstream.h> // For file I/O
int main()
{
int height;
int width;
ifstream inFile;
inFile.open(mydata.dat); // Attempt to open input file
if ( !inFile ) // Was it opened?
{
cout << Can't open the input file.; // No--print message
return 1; // Terminate program
}
inFile >> height >> width;
.
.
.
return 0;
} |
|
|
|
|
|
|
|
|
In this program, we begin by attempting to open the disk file mydata.dat for input. Immediately, we check to see whether the attempt succeeded. If it was successful, the value of the expression !inFile in the If statement is zero (FALSE) and the then-clause is skipped. The program proceeds to read |
|
|
|
|
|