Dr Nick said:
What does errno (or perror for that matter) show the actual problem to
be?
Is the directory empty?
Note that surprisingly few standard functions are required *by the C
standard* to set errno on failure. Of the <stdio.h> functions, only
fgetpos(), fsetpos(), and ftell() are explicitly required to do so, and
they all set it to an implementation-defined positive value (in
addition, I/O functions set errno to EILSEQ on an encoding error.
But on most systems, most or all <stdio.h> functions probably set errno
to a meaningful value on failure. POSIX has stronger requirements for
this than the C standard does.
Conclusion: You can probably count on a meaningful errno after an I/O
function has failed (assuming you set errno to 0 before the call), but
if you don't get one you probably can't blame the system for failing to
conform to the C standard.