ç读取文件的权限 [英] c read a file's permissions

查看:116
本文介绍了ç读取文件的权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查文件是否已用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).

这篇关于ç读取文件的权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆