I have this C code:
FILE * fd = fopen(filename,"rb");
printf("%c ",(char)getc(fd)); // returns expected char
unsigned char buffer[10];
printf("%d ",read(fd, &buffer, 10)); // -1
printf("%d\n",errno); // 9
getc
returns a char from the input file, as expected. However read
returns an error (-1) and errno
is set to 9 (bad file descriptor). Clearly the file descriptor is OK, since getc
manages to use it to read a char.
What is the problem here?