Open the file in Linux. I do not want to create a write protected file

I have a problem creating a file on Linux, it makes my file write protected and I don't know why it does it.

void fileOperation::openFileWrite(char x, off_t s)
{
  int fd;
  char c[2] = {x};

  fd = open("/home/stud/txtFile", O_CREAT | O_WRONLY);  //open file
  if(fd == -1)
      cout << "can't open file" << endl;
  else
  {
      lseek(fd, s, SEEK_SET);//seek at first byte
      write(fd, (void*)&c, 2);//write to file
  }
  syncfs(fd);
  ::close(fd);
}

      

+3


source to share


2 answers


You must use an optional argument with a set of write permissions (the default permission for you may deny write permission)

 fd = open("/home/stud/txtFile", O_CREAT | O_WRONLY, 0666);//open file

      



0666 is an octal number, that is, each of 6 corresponds to three bits of resolution

6 = rw
7 = rwx

      

+4


source


You probably have a limiting one umask

. The call open

will simply try to create a file with mode 0666, but the umask user will usually remove many of these permission bits.



+1


source







All Articles