0

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?)

Sathyajith Bhat
  • 21,321
  • 22
  • 95
  • 134
Ben J
  • 1,367
  • 2
  • 15
  • 33
  • 2
    This sort of information is very OS dependent so there's probably no real way to tell. I've don't recall there being a "tape not in tape drive" error code or even a "file on different planet". – Skizz Sep 12 '12 at 15:21

1 Answers1

2

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.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243