Warm tip: This article is reproduced from serverfault.com, please click

gcc-将C中的文件权限设置为只读

(gcc - Setting Permissions to a file in C to Read Only)

发布于 2020-11-30 03:22:50

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

OS是使用GCC的Raspbian 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);

             }
}




Questioner
YHapticY
Viewed
0
661k 2020-11-30 13:14:55

似乎你使用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调整口味。