Trying to know the basic IO function in POSIX, I wrote the following code, but it does not work and a "bad file descriptor" error is given when I try to execute the code:
#include & lt; Stdio.h & gt; # Include & lt; Stdlib.h & gt; # Include & lt; Sys / stat.h & gt; # Include & lt; Fcntl.h & gt; # Include & lt; Unistd.h & gt; Int main (zero) {int nfd; Ssize_t ret; Mode_t mode = S_IRWXU | S_IRWXG; Nfd = openat (AT_FDCWD, "idx.txt", O_APPEND | O_SYNC | O_CREAT, mode); If (-1 == NFD) {copy ("open ()"); Exhaust (EXIT_FAILURE); } Rate = Write (NFD, "Hello", 5); If (-1 == reserved) {mirror ("write ()"); Exhaust (EXIT_FAILURE); } Pass (NFD); Return 0; }
I want to write the file in O_APPEND mode. But:
$ idid.txt.txt # This file does not already exist, even if $. / .out write (): Bad file descriptor