Per verificare se sei giunto alla fine del file usa feof(fd); EOF è una macro che non c'entra.
Amaro C++, il gusto pieno dell'undefined behavior.
Regole del Forum