3

I have a problem when I create a file in Linux it makes my file write-protected and I don't know why it does that.

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);
}
manlio
  • 18,345
  • 14
  • 76
  • 126
Loc Dai Le
  • 1,661
  • 4
  • 35
  • 70

2 Answers2

5

You have to use additional argument with write permission set (default permission for you may be taking write permission off)

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

0666 is an octal number, i.e. every one of the 6's corresponds to three permission bits

6 = rw
7 = rwx
Dr. Debasish Jana
  • 6,980
  • 4
  • 30
  • 69
1

You probably have a restrictive umask. The open call will simply attempt to create a file with mode 0666 but the user's umask typically removes many of those permission bits.

tripleee
  • 175,061
  • 34
  • 275
  • 318