0

I'm new to C programming and I'm trying to experiment with setting the permissions of a file to Read Only. I'm sure that I don't have the directives correct and when I try to compile I get the error on the line that #include <io.h> is on "fatal error: io.h no such file or directory". The file 'time.log' is in a directory called 'time_logs' and the program will run from the same directory that the directory 'time_logs' is in.

OS is Rasbian for Raspberry Pi 4 Arm Using GCC

#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);

             }
}




YHapticY
  • 177
  • 11
  • 1
    your question statement and description does not match – csavvy Nov 30 '20 at 03:29
  • 2
    I assume you are coding in Windows? If so, suggest adding that to the description and tags. Also including how you are compiling would be helpful. – kaylum Nov 30 '20 at 03:32
  • What OS are you using? And why are you calling that `_chmod()` function twice with the same arguments? – Shawn Nov 30 '20 at 03:34
  • I'm coding on Raspberry Pi 4 Arm Architecture. using Arm gcc. I probably picked up the wrong directives when doing research, but im coding in Raspberry Pi 4 Raspbian Arm. I'm not completely familiar with how _chmod works. I've used it with Linux commands and did some research on how to change permissions with C for files and _chmod was used a few times. this was my best guess that I could come up with for manipulating the permissions of the file. – YHapticY Nov 30 '20 at 03:46
  • 1
    You must be reading some Microsoft documentation. That code is not valid for Linux. Suggest you search more specifically "linux C change file permission" – kaylum Nov 30 '20 at 03:48

1 Answers1

1

It looks like you used a Windows manual trying to code for 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");

But most people just type the permission bits directly. This would be 0444. Adjust to taste.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Joshua
  • 40,822
  • 8
  • 72
  • 132