【发布时间】:2014-07-08 08:54:44
【问题描述】:
我想在 linux 中使用 c++ 创建一个具有特定文件权限 (1644) 的文件。我知道我可以使用 chmod 来实现这一点,但我想通过 c++ 以编程方式进行。
这可能吗?请帮忙。
谢谢。
【问题讨论】:
我想在 linux 中使用 c++ 创建一个具有特定文件权限 (1644) 的文件。我知道我可以使用 chmod 来实现这一点,但我想通过 c++ 以编程方式进行。
这可能吗?请帮忙。
谢谢。
【问题讨论】:
你需要在 sys/stat.h 中使用 struct stat
man 2 stat 查看各种 st_mode 字段值。
【讨论】:
您可以使用 sys/stat.h 中的 chmod 函数:
int chmod(const char *path, mode_t mode);
比如:
#include <sys/stat.h>
...
if (!chmod("/tmp/testfile",
S_ISVTX | // Sticky bit
S_IRUSR | // User read
S_IWUSR | // User write
S_IRGRP | // Group read
S_IROTH // Other read
))
{
// Handle error case
}
【讨论】: