在 linux 中打开文件。我不想创建写保护文件

Loc*_* Le 3 c++ linux unistd.h

我在 Linux 中创建文件时遇到问题,它使我的文件被写保护,我不知道为什么会这样。

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);
}
Run Code Online (Sandbox Code Playgroud)

Dr.*_*ana 5

您必须使用具有写权限集的附加参数(您的默认权限可能会关闭写权限)

 fd = open("/home/stud/txtFile", O_CREAT | O_WRONLY, 0666);//open file
Run Code Online (Sandbox Code Playgroud)

0666 是一个八进制数,即 6 中的每一个都对应三个权限位

6 = rw
7 = rwx
Run Code Online (Sandbox Code Playgroud)