问题
What is the best way to get meaningful file access error messages, in a portable way from std::fstreams ? The primitiveness of badbits
and failbits
is getting to be bit annoying. I have written my own exception hierarchies against win32 and POSIX before, and that was far more flexible than the way the STL does it.
I am getting "basic::ios_clear" as an error message from the what
method of a downcasted catch (std::exception
) of a fstream
which has exceptions enabled. This doesn't mean much to me, although I do know what the problem is I'd like my program to be a tad more informative so that when I start deployment a few months later my life will be easier.
Is there anything in Boost to extract meaningful messages out of the fstream
's implementation cross platform and cross STL implementation ?
回答1:
Nobody stops you from also checking errno
/strerror
(e.g. in your exception handler) for a more specific reason for failure.
UPDATE -- regarding portability
Incidentally, IIRC Visual Studio's fstream
implementation calls the _open
/_read
/_write
/etc. CRT methods, which set errno
. Microsoft makes no guarantee about GetLastError
still containing the correct value after the CRT methods return. Idem for the cygwin, mingw etc. implementations, which set errno
with no claims or guarantees about GetLastError
.
So I stand by my claim that all you need, can, and therefore want to do is check errno
.
Now, given all of the above, if you still want to complicate your life and overengineer by using Boost::System
instead of simply calling strerror
then I guess my definition and your definition of elegance and simplicity are not the same. :)
回答2:
What information do you want? badbit
indicates an I/O error. eofbit
indicates eof. failbit
indicates a parse error.
To eliminate one solution, anyway, I don't think you can override the native-type input functions because of ADL. You could implement operator>>(istream, input_safe_int)
where input_safe_int
is constructed from int&
. Put a try
block inside, etc.
来源:https://stackoverflow.com/questions/2639324/getting-meaningful-error-messages-from-fstreams-in-c