I have been having trouble reading from a file I created in c with the creat system call. Here is the code:
XXXfid = creat ("XXX", 384);
if (XXXfid<0) {printf("Error with creat"); return 0;}
writeStatus = write (XXXfid, "limp", 4);
if (writeStatus<0) {printf("error with write"); return 0;}
lseek(XXXfid, 0, 0);
readStatus = read(XXXfid, buffer, 120);
if (readStatus<0) {printf("error with read2"); return 0;}
Every other command is working perfectly, I just can't read the file no matter what I try (ie closing and re-opening, etc). I have read and write permissions, do I need to give everyone read and write permissions? I wouldn't have thought so, given that the error I'm getting is 9- Bad File Descriptor. I'm at a total loss, any help would be appreciated I believe in octal notation my permission would be 600. If it helps, this is 110 000 000 in binary and my ls -l confirms this is -rw- --- ---
According to the specification:
creat(path, mode)
is equivalent to
open(path, O_WRONLY|O_CREAT|O_TRUNC, mode)
Notice that the flags contain O_WRONLY
, not O_RDWR
. This means you can only write to the file descriptor, not read from it.
If you want to read and write, use open()
instead, with the appropriate flags.
XXXfid = open("XXX", O_RDWR | O_CREAT | O_TRUNC, 0600);