【问题标题】:Setting Permissions to a file in C to Read Only将 C 中文件的权限设置为只读
【发布时间】:2021-03-12 00:42:39
【问题描述】:

我是 C 编程新手,我正在尝试将文件的权限设置为只读。我确定我没有正确的指令,当我尝试编译时,我在#include 位于“致命错误:io.h 没有这样的文件或目录”的行上得到错误。文件 'time.log' 位于名为 'time_logs' 的目录中,程序将从目录 'time_logs' 所在的同一目录中运行。

操作系统是使用 GCC 的 Raspberry Pi 4 Arm 的 Rasbian

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <io.h>
#include <sys.h>


struct stat st = {0};

int main(void){


      if(_chmod("time_logs/time.log", _S_IREAD) == -1)
         perror("Not found");

        else{
              _chmod("time_logs/time.log", _S_IREAD);

             }
}




【问题讨论】:

  • 您的问题陈述与描述不符
  • 我假设您是在 Windows 中编码?如果是这样,建议将其添加到描述和标签中。还包括您的编译方式会有所帮助。
  • 您使用的是什么操作系统?为什么要用相同的参数两次调用 _chmod() 函数?
  • 我正在使用 Raspberry Pi 4 Arm 架构进行编码。使用 Arm gcc。我可能在研究时选择了错误的指令,但我在 Raspberry Pi 4 Raspbian Arm 中编码。我并不完全熟悉 _chmod 的工作原理。我已经将它与 Linux 命令一起使用,并对如何使用 C 更改文件的权限进行了一些研究,并且 _chmod 被使用了几次。这是我对操纵文件权限的最佳猜测。
  • 您一定正在阅读一些 Microsoft 文档。该代码对 Linux 无效。建议您更具体地搜索“linux C更改文件权限”

标签: c gcc permissions raspberry-pi4 raspbian-buster


【解决方案1】:

看起来您使用 Windows 手册尝试为 Linux 编写代码。

#include <unistd.h>
#include <sys/stat.h>

      if(chmod("time_logs/time.log", S_IRUSR | S_IRGRP | S_IROTH) == -1)
         perror("time_logs/time.log");

但大多数人只是直接输入权限位。这将是0444。调整口味。

【讨论】:

  • 是的,我似乎在使用一些 Windows 指令。不过,我正在使用 Linux。所以我以后一定会搜索Linux C。
  • @YHapticY:提示:搜索 linux man 2 xyzlinux man 3 xyz
  • 非常感谢@Joshua 的提示。我想我现在可以工作了。
猜你喜欢
  • 1970-01-01
  • 2017-01-29
  • 2014-07-31
  • 2019-02-09
  • 2013-05-11
  • 2011-11-27
  • 1970-01-01
  • 2016-01-02
  • 2011-08-15
相关资源
最近更新 更多