L
lawrence.jones
Nick Keighley said:hmmm. The Standard does not guarantee that fopen() sets errno
(which is what the machinary of perror() uses underneath). But
many implementaions *do* set errno. This includes such obscure
platforms as Windows and most (all?) Unix platforms. This is one
place where I accept the technically non-portability and call
perror() after a failed fopen() (well actually I prefer strerror()).
Traditional stdio implementations set errno to a reasonable value
somewhat coincidentally in most failure cases, but not all of them. If
you hit one of the rare cases where it did not, you would get a leftover
value from some unrelated failure, either from within fopen itself or
from a previous call, which can be quite puzzling (the most common
leftover value is ENOTTY: Not a tty). So, you have to balance the
advantage of potentially getting helpful information against the
disadvantage of potentially getting misleading information. I generally
come to the same conclusion that you do -- the gain outweighs the rare
pain.