is_open()
The C++ file stream classes inherit a stream-state member from the ios_base
class. This member, as discussed earlier, stores information that reflects the stream status: All is well, end-of-file has been reached, I/O operation failed, and so on. If all is well, the stream state is zero (no news is good news). The various other states are recorded by setting particular bits to 1
. The file stream classes also inherit the ios_base
methods that report about the stream state and that are summarized in Table 17.4. You can check the stream state to find whether the most recent stream operation succeeded or failed. For file streams, this includes checking the success of an attempt to open a file. For example, attempting to open a non-existent file for input sets failbit
. So you could check this way:
fin.open(argv[file]);
if (fin.fail()) // open attempt failed
{
...
}
Or because an ifstream
object, like an istream
object, is converted to a bool
type where a bool
type is expected, you could use this:
fin.open(argv[file]);
if (!fin) // open attempt failed
{
...
}
However, newer C++ implementations have a better way to check whether a file has been opened—the is_open()
method:
if (!fin.is_open()) // open attempt failed
{
...
}
The reason this is better is that it tests for some subtle problems that the other forms miss, as discussed in the following Caution.
In the past, the usual tests for successful opening of a file were the following:
if(fin.fail()) ... // failed to open
if(!fin.good()) ... // failed to open
if (!fin) ... // failed to open
The fin
object, when used in a test condition, is converted to false
if fin.good()
is false
and to true
otherwise, so the two forms are equivalent. However, these tests fail to detect one circumstance, which is attempting to open a file by using an inappropriate file mode (see the “File Modes” section, later in this chapter). The is_open()
method catches this form of error, along with those caught by the good()
method. However, older C++ implementations do not have is_open(
).
18.116.60.62