【问题标题】:how to set file attributes(1644) to a particular file using c++ on linux如何在 Linux 上使用 c++ 将文件属性(1644)设置为特定文件
【发布时间】:2014-07-08 08:54:44
【问题描述】:

我想在 linux 中使用 c++ 创建一个具有特定文件权限 (1644) 的文件。我知道我可以使用 chmod 来实现这一点,但我想通过 c++ 以编程方式进行。

这可能吗?请帮忙。

谢谢。

【问题讨论】:

    标签: c++ linux file chmod


    【解决方案1】:

    使用stat(2) 检索权限,然后使用chmod(2) 更改它们。

    更一般地说,要了解某些(命令行)程序(例如/bin/chmod...)执行的系统调用,请使用strace(1)....

    【讨论】:

      【解决方案2】:

      你需要在 sys/stat.h 中使用 struct stat

      man 2 stat 查看各种 st_mode 字段值。

      【讨论】:

        【解决方案3】:

        您可以使用 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
        }
        

        【讨论】:

          猜你喜欢
          • 2019-02-09
          • 1970-01-01
          • 1970-01-01
          • 2015-11-04
          • 2018-09-02
          • 1970-01-01
          • 1970-01-01
          • 2013-01-07
          • 1970-01-01
          相关资源
          最近更新 更多