If you are using C++11, you can turn on exceptions, open the file and catch the exception. The ios_base::failure excepion should be derived from std::system_error and have a std::error_code associated with the exception. This should provide the low-level details of the error.
If you are using C+03 on Unix, errno should be set and you can get the reason with strerror(errno).
I always prefer to call the exceptions() member function with at least badbit. badbit is supposed to signal an I/O error of some sort; failbit other issues.
Note that my implementation (Linux 4.7.6, glibc 2.4.1) doesn't return a useful exception description in what(); mine does set errno correctly.
The implementation isn't required to set errno. Under Windows, maybe GetLastError() will yield results.
I turned on exceptions as per your suggestion PanGalactic, but there is not any meaningfull information to be obtained (there is only one error code for ios_base::failure exceptions)...
So? Am I missing something? I know my code is trying to create a file with a name with invalid characters in it ... but I found that by inspection and thought, not by any error code...
I tried GetLastError() and got "Broken Pipe" when opening a file for writing when the file is already open by another application so it is unaccesible. Does this message make sense?
So? Am I missing something? I know my code is trying to create a file with a name with invalid characters in it ... but I found that by inspection and thought, not by any error code...
I would do as mbozzi illustrates. There is no restriction on characters in a C++ file name. Such restrictions come from the operating system and the exceptions generated by streams tend to only indicate success or failure, not the causse of the failure (possibly because those reasons are not enumerated by the standard either, but by the operating system.) So going to std::strerror for more details makes sense.