Determining the exact reason for not opening of file
-
01-07-2021 - |
Pregunta
The problem is that std::fstream
doesn't throw exceptions by default but rather sets bits that can then be examined. Apparently, it can then be made to throw exceptions (I think) by using the exceptions function as explained here -- see e.g. c++ reference page
But what if, for example, the write permissions of the file mean that the file cannot be written to? This would mean that when I try and do
ofstream file("file", ios::out);
a failure would result. But how can it be determined if it failed for the precise reason that the uid didn't have write permissions? I guess what I am looking for is some mechanism that will tell me precisely this e.g. it might show "File cannot be written to because...". I do not want to have to check file permissions because there are so many reasons why write failure could occur (hard-drive failures/corruption etc.). It would be better if it would just tell me exactly why it failed.
Does anybody know if such an error checking system exists for iostreams (ostreams in particular)? (Maybe in boost?)
Solución
You can try to check errno
and perhaps convert it to a human-readable form with strerror
.
The standard doesn't guarantee anything about applicability of errno
to I/O failures, but in practice it should work.