Can you get a specific error condition when opening a C ++ stream?

Is there a way to get a specific error condition when opening a C ++ stream? That is, whether it failed because the file did not exist, or the permissions were incorrect, etc. I am mainly looking for functionality equivalent to errno for fopen () in simple C.

GCC seems to set errno correctly, but it does not look like the C ++ standard, and I cannot determine if this is just an artifact of how they implemented the threads, or an intentional function (and therefore I don't know if it is saved in different versions).

Is there a way to get this information reliably, either in standard C ++, or not by accident in one or more main compilers?

+12
c ++ stream
Nov 20 '08 at 0:28
source share
2 answers

You can see the ios flags (badbit, eofbit, failbit, goodbit) for common reasons. Testing will be easier with ios :: bad (), ios :: fail (), ios :: eof () or ios :: good (). A thread can also be set to throw exceptions on error using ios :: exceptions ().

Detailed I / O error reporting may be available in some implementations as you point to GCC. You may need to rely on this behavior for different compilers. If there is a chance for multiple compilers, be sure to check and probably include preprocessor instructions to check the current compiler, etc.
As far as I know, the only other place he discussed is in the proposed TR2 add-ons .

+4
Nov 20 '08 at 3:37
source share

In Visual Studio fopen, etc. set the value of the last error code. It can be found using GetLastError () . The result will be one of these values .

0
Nov 20 '08 at 1:37
source share



All Articles