I have a while (!feof(infile)) loop, so that exits only when the eof is reached. And it closes all files upon exit. Here's the code:
// scan input file
while (!feof(infile))
{
// read four bytes
BYTE byte[4];
fread(&byte, sizeof(BYTE), 4, infile);