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
我懷疑我打開文件的方式是不正確的。我想打開文件以便寫作和閱讀。該文件已存在。我錯過了什麼?
它們不會'''很好地結合在一起,因爲最初的「模式」只是一個布爾值,表示您是否需要寫入權限。讀寫模式不存在。添加時,爲了兼容性,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