How can I check if a file has read permissions in C?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Use
access(2)in POSIX. In Standard C, the best you can do is try to open it withfopen()and see if it succeeds.If
fopen()returnsNULL, you can try to useerrnoto distinguish between the “File does not exist” (errno == ENOENT) and “Permission denied” (errno == EACCES) cases – but unfortunately those twoerrnovalues are only defined by POSIX as well.(Even on POSIX, in most cases the best thing to do is try to open the file, then look at why it failed, because using
access()introduces an obvious race condition).