c++ - 在 Linux 中打开文件。我不想创建写保护文件

标签 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);
}

最佳答案

您必须使用额外的参数设置写权限(您的默认权限可能会取消写权限)

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

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

6 = rw
7 = rwx

关于c++ - 在 Linux 中打开文件。我不想创建写保护文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26544214/

相关文章:

c++ - 使用 union 会有好处吗?

C NET-SNMP 专门通过 MIB 名称获取和设置,而不是 OID

java - 在 Linux 上找不到主类 - 类路径问题

c++ - 原子附加到文件描述符上,但偏移量是多少?

c - 如何选择其中一个选项?

c - C 中的错误代码

c++ - QGraphicsItem::paint():如何检查 QGraphicsScene 是否被打印

c++ - 哪个是管理映射到其他一些小对象的小对象的正确容器?

c++ - 在 C++ 中制作对象列表

linux - 需要将以下 awk 命令转换为 sed