What you want to do is not possible in standard C++. You seem to think that a file stream knows if the external device is available if you remove it between construction and destruction of the stream. In reality, all you're doing is invoking undefined behavior by writing to a device that you've told the stream exists and will continue to exist until destruction. That's an implied contract when you open a file stream and you're not adhering to it.
My kneejerk reaction to this problem is to tell you not to delete the file until you're done with it. However, you can do a platform dependent test to see if the file exists before every operation on it. However, that's inefficient and just sweeps the dirt under the rug.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>So what you're saying is that it can't detect the deletion, or a change in permissions, etc
Did I studder?
What you want to do is not possible in standard C++.
>What about running out of disk space
That will cause failbit (and possibly badbit) to be set.
>By the way, if not for detecting problems writing, what's the point of the .bad() and .fail() functions?
You're confusing errors during legitimate use with blatant and intentional breaking of the rules.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>should they ever occur it will at least throw an exception.
Only if you tell it to either through an explicit test:
if (stream.fail())
throw <em>some exception</em>;
Or tell the stream to throw exceptions (which it doesn't do by default):
stream.exceptions(ios_base::failbit | ios_base::bad_bit);
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401