Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ifstream::is_open vs ifstream::fail?

Tags:

c++

ifstream

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?

like image 713
OJFord Avatar asked Jun 07 '14 13:06

OJFord


People also ask

What does Is_open () do in C++?

std::ifstream::is_openReturns whether the stream is currently associated to a file. Streams can be associated to files by a successful call to member open or directly on construction, and disassociated by calling close or on destruction.

How do I check if a file is successfully open C++?

You can check if a file has been correctly opened by calling the member function is_open(): bool is_open(); that returns a bool type value indicating true in case that indeed the object has been correctly associated with an open file or false otherwise.

What is the difference between ifstream and Fstream?

Ifstream: File handling class that signifies the input file stream and is used for reading data from the file. Fstream: File handling class that has the ability to handle both ifstream and ofstream. It can be used to read from and write to a file.

Does ifstream open file?

std::ifstream::open. Opens the file identified by argument filename , associating it with the stream object, so that input/output operations are performed on its content. Argument mode specifies the opening mode. If the stream is already associated with a file (i.e., it is already open), calling this function fails.


2 Answers

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.

like image 185
Filip Roséen - refp Avatar answered Sep 19 '22 20:09

Filip Roséen - refp


Use the bool conversion operator instead!

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

Or better:

ifstream i("test.txt"); if (!i) {     //failure, handle error } 
like image 45
Csq Avatar answered Sep 20 '22 20:09

Csq