I'm taking Intro to Programming. Today we learned about using fstream to read/write to the filesystem. The teacher mentioned "fail states", saying they can happen for a variety of reasons, including the file not existing.
Let's say I have a program where a fail state happens. Obviously, if a fail state happens, the program should act appropriately, but first it must know why the fail state happened.
Stream failure can happen for a lot of reasons, but C++ isn't interested in most of them, only that something went wrong.
No matter what stream, or how it is opened, you should test its state after every read attempt. If any kind of failure occurs, you can test what that specific class of error is, either by querying the iostate directly or using one of the accessor functions like bad() and fail().