问题描述
如何检查文件是否已用C读取权限?
How can I check if a file has read permissions in C?
推荐答案
使用接入(2)
在POSIX。在标准C,你能做的最好的就是尽量与的fopen打开它()
,看看它是否成功。
Use access(2)
in POSIX. In Standard C, the best you can do is try to open it with fopen()
and see if it succeeds.
如果 fopen()函数
收益 NULL
,你可以尝试使用错误号
文件不存在区别开来(的errno == ENOENT
)和权限被拒绝(的errno == EACCES
)的情况下 - 但不幸的是这两个错误号
值仅由POSIX定义以及
If fopen()
returns NULL
, you can try to use errno
to distinguish between the "File does not exist" (errno == ENOENT
) and "Permission denied" (errno == EACCES
) cases - but unfortunately those two errno
values are only defined by POSIX as well.
(即使在POSIX,在大多数情况下,最好的办法是尝试打开该文件,再看看为什么会失败,因为使用接入()
引入了明显的竞争条件)。
(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).
这篇关于ç读取文件的权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!