Reputation: 73444
The file exists and I have just read from it in another function. The other function closes the file. Now, my workwith()
tries to open it and read from it.
My code:
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);
}
}
However, I am getting this output:
file_descriptor = 3, filename = |t.mfs|
read superblock: Bad file descriptor
I suspect that the way I am opening the file is not correct. I want to open the file for writing and reading purposes. The file already exists. What am I missing?
Upvotes: 0
Views: 72
Reputation: 53036
Change this flags
O_WRONLY | O_RDONLY
to
O_RDWR
check here, it says that the flags must include one of the access modes.
Moreover, the ref mentions:
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.
Upvotes: 2