2015-02-09 59 views
0

该文件存在,我刚刚从另一个函数中读取。另一个函数关闭文件。现在,我的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 

我怀疑我打开文件的方式是不正确的。我想打开文件以便写作和阅读。该文件已存在。我错过了什么?

回答

3

更改此标志

O_WRONLY | O_RDONLY 

O_RDWR 

检查here,它说,该标志必须包括接入方式一个

此外,裁判提到:

的参数标志必须包括以下接入模式中的一种: O_RDONLY,O_WRONLY或O_RDWR。这些请求分别仅打开文件read- ,只写或读/写。

+1

它们不会'''很好地结合在一起,因为最初的“模式”只是一个布尔值,表示您是否需要写入权限。读写模式不存在。添加时,为了兼容性,readonly的数值保持为0。 [V3手册页](http://minnie.tuhs.org/cgi-bin/utree.pl?file=V3/man/man2/open.2)[V4手册页](http://minnie.tuhs。 org/cgi-bin/utree.pl?file = V4/man/man2/open.2)...我认为Ken和Dennis很聪明,知道'O_RDWR!= O_RDONLY | O_WRONLY'很愚蠢,并且会修复如果他们能的话。 – 2015-02-09 14:54:11