#How to error handle readdir with perror?

3 messages · Page 1 of 1 (latest)

surreal idolBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

deep zodiac
#

check the manpage:

RETURN VALUE
       On success, readdir() returns a pointer to a dirent structure.  (This structure may be statically allocated; do  not  attempt  to
       free(3) it.)

       If  the  end of the directory stream is reached, NULL is returned and errno is not changed.  If an error occurs, NULL is returned
       and errno is set to indicate the error.  To distinguish end of stream from an error, set errno to zero before  calling  readdir()
       and then check the value of errno if NULL is returned.

so set errno = 0 before using readdir and check if errno still == 0 when readdir returns NULL (after the while loop). then handle the error how ever you want.

surreal idolBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity