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

后端 未结 2 1673
忘掉有多难
忘掉有多难 2021-01-04 07:33

Is there any way to get a specific error condition when a C++ stream open fails? That is, whether it failed because the file didn\'t exist, or permissions were wrong, or etc

相关标签:
2条回答
  • 2021-01-04 08:05

    You can look at the ios flags (badbit, eofbit, failbit, goodbit) for general reasons. Testing will is easier using ios::bad(), ios::fail(), ios::eof(), or ios::good(). The stream can also be set to generate exceptions on error, using ios::exceptions().

    Detailed I/O Error Reporting may be available in some implementations, as you point out for GCC. You may have to rely on this behavior for the different compilers. If there is a chance for multiple compilers, make sure to test and probably include preprocessor statements to check the current compiler, etc.
    As far as I know, the only other place it's discussed is in proposed TR2 additions.

    0 讨论(0)
  • 2021-01-04 08:22

    In Visual Studio fopen and the like set the last-error code value. It can be retrieved with GetLastError(). The result will be one of these values.

    0 讨论(0)
提交回复
热议问题