32

Reading Savitch's Problem Solving in C++, std::ifstream::fail is shown as an example to check if a file has been correctly opened (ifstream or ofstream).

I've previously used, as it is what I was first shown, std::ifstream::is_open to perform the same check.

Which is 'better' practice?

Or in the case that either one is called directly after attempting to open, does it make no practical difference?

Filip Roséen - refp
  • 62,493
  • 20
  • 150
  • 196
OJFord
  • 10,522
  • 8
  • 64
  • 98
  • Open to tag suggestions if you think necessary. Couldn't find an appropriate 'best-practice' or 'coding-style' type tag. – OJFord Jun 07 '14 at 13:13

2 Answers2

33

INTRODUCTION

std::ifstream::fail includes checking std::ifstream::is_open, but std::ifstream::is_open only checks if it was possible to create a handle to the file.


EXPLANATION

std::ifstream::fail can return true, even if std::ifstream::is_open returns true; they are not the mutually exclusive.

.fail will check the overall "health" of the stream, which involves things such as checking the stream has currently entered a fail state from trying to read an invalid value, whereas .is_open will only check if the stream is currently attached to a file, .is_open doesn't care if the stream is in a fail state, or not.


WHAT'S THE BETTER PRACTICE?

This certainly depends on what you are trying to accomplish.

Normally it's recommended to rely on the explicit operator bool () to see if a stream is ready to be read/written to. This includes checking the overall health of the stream.

Can we make another read/write operation on some_stream?

if (some_stream) {
  // stream is alive and well
} else {
  // something is wrong
}

If you explicitly would like to see if some fstream is actually attached to a file, use is_open, and if you want to check the overall health; use .fail or rely on the fact that a stream is convertiable to bool.

Community
  • 1
  • 1
Filip Roséen - refp
  • 62,493
  • 20
  • 150
  • 196
  • It explains the difference between `is_open()` and `fail()` in detail, but still does not answer the main question of OP: is there any difference when using both _just after file stream object creation_. Specifically, are there and what are the cases when `fail() && is_open()` just after file stream creation. – Alex Che Aug 05 '22 at 17:09
13

Use the bool conversion operator instead!

ifstream i("test.txt");
if (i) {
    //success
}

Or better:

ifstream i("test.txt");
if (!i) {
    //failure, handle error
}
Csq
  • 5,775
  • 6
  • 26
  • 39
  • I didn't know that could be done. What makes it better? If the file is open but empty, does it return true? – OJFord Jun 07 '14 at 13:14
  • 3
    @OllieFord as the [reference says](http://en.cppreference.com/w/cpp/io/basic_ios/operator_bool), it's essentially a nice shordhand for `!fail()`. It is good, because it's easy to remember and it calls the correct function to check if the stream is ok. (and not `eof()`, `bad()`, etc.) – Csq Jun 07 '14 at 13:19
  • why second one is better? – Abhinav Gauniyal Aug 31 '16 at 18:10
  • 2
    @AbhinavGauniyal Because a rest of code will be written for successful case, therefore you will have to put a lot of code in condition, but it will be bad readable. So it is better to just make a few actions in the fail condition and write your rest code without additional block of code – FreePhoenix888 Apr 05 '21 at 10:38