无法从打开的文件中读取

Can not read from opened file

该文件存在,我刚刚在另一个函数中读取了它。另一个函数关闭文件。现在,我的 workwith() 尝试打开它并读取它。

我的代码:

if (access(path_file, F_OK) != -1) {
  // file exists
  *mfs_desc = open(path_file, O_WRONLY | O_RDONLY, 0600);
  if (*mfs_desc == -1) {
    perror("opening file");
    exit(1);
  }
  printf("file_descriptor = %d, filename = |%s|\n", *mfs_desc,
         path_file);
  if ((read(*mfs_desc, superblock, sizeof(Superblock))) == - 1) {
    perror("read superblock");
    exit(1);
  }
}

但是,我得到了这个输出:

file_descriptor = 3, filename = |t.mfs|
read superblock: Bad file descriptor

我怀疑我打开文件的方式不正确。我想打开文件进行写入和读取。该文件已经存在。我错过了什么?

更改此标志

O_WRONLY | O_RDONLY

O_RDWR

检查here,它表示标志必须包括一种访问模式。

此外,裁判提到:

The argument flags must include one of the following access modes: O_RDONLY, O_WRONLY, or O_RDWR. These request opening the file read- only, write-only, or read/write, respectively.