You tried to read from, or write to, the file descriptor fd which was not open for that operation.
Open the file descriptor fd for the appropriate operation.