如何确定文件在* nix上是否为只读文件? [英] How can I determine if a file is read-only for my process on *nix?
本文介绍了如何确定文件在* nix上是否为只读文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用stat函数,我可以获得以下内容的读/写权限:
Using the stat function, I can get the read/write permissions for:
- 所有者
- 用户
- 其他
...但这不是我想要的.我想知道文件对我的进程(即我正在编写的应用程序)的读/写权限.仅当我知道我的进程是否以文件的所有者/用户/其他身份运行时,所有者/用户/其他才有帮助...所以也许这是解决方案,但我不确定到达那里的步骤.>
...but this isn't what I want. I want to know the read/write permissions of a file for my process (i.e. the application I'm writing). The owner/user/other is only helpful if I know if my process is running as the owner/user/other of the file...so maybe that's the solution but I'm not sure of the steps to get there.
推荐答案
您不想为此使用stat()
.您要使用<unistd.h>
中的access()
:
You don't want to use stat()
for this. You want to use access()
from <unistd.h>
:
char const* name = "file";
if (access(name, R_OK)) {
std::cout << "'" << name << "' is readable\n";
}
if (access(name, W_OK)) {
std::cout << "'" << name << "' is writable\n";
}
这篇关于如何确定文件在* nix上是否为只读文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文