如何在linux上使用c++为特定文件设置文件属性(1644)
how to set file attributes(1644) to a particular file using c++ on linux
我想在linux中使用c++创建一个具有特定文件权限(1644)的文件。我知道我可以用chmod实现这一点,但我想通过c++编程地做到这一点。
这可能吗?请帮助。
谢谢。
使用stat(2)获取权限,然后使用chmod(2)更改权限
更一般地说,要了解某些(命令行)程序(例如/bin/chmod
…)执行的系统调用,请使用strace(1)....
你需要在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
}
相关文章:
- 生成文件不对文件使用隐式规则
- C++ 读取文件读取文件不正确
- C++(.cpp文件和.h文件)拆分代码并添加一个函数,提取 - 这很容易吗?
- 使用 Powershell 命令将 cpp 文件的文件夹编译为 GNU 的 g++
- 如何识别项目是 QT 中的文件还是文件夹
- 如何将包含另一个对象向量的对象保存到文件中,并使用C++中的二进制文件从文件中读回?
- 文件模式标志"ios::app"是否用于删除文件(如果文件已存在)?
- 如何将我的程序添加到文件和文件夹的macOS右键单击菜单?
- 即使使用 FILE_FLAG_DELETE_ON_CLOSE 属性创建文件,文件也会保留在磁盘上
- 如何从C 中的HDF5文件中读取属性名称和数据集名称,在这里我不知道HDF5文件中存在什么属性
- 使用 CDT 从头文件中获取属性定义的节点元素是什么
- 如何在 QML 文件中查找属性名称为字符串的 QML 上下文属性
- c++ 从配置文件生成类属性
- 获取 XML 文件的特定属性
- 从C++中的QML文件加载属性
- 使用 boost::spirit 解析二进制文件时更改属性类型
- 从json文件构造boost属性树的性能非常差
- 正在重置文件安全性以继承MoveFile()操作之后的文件
- 如何在linux上使用c++为特定文件设置文件属性(1644)
- 通过.rc文件设置dll属性:Windows 7显示哪些属性