// Read each chunk in turn and process if it's a track chunk
FILE *fp;
// ...open the file, blah blah...
// Each chunk consists of this header, followed by some data
struct midi_chunk_t
{
union {
char c_id[4]; // Four-character chunk identification
unsigned int id; // As a 32-bit integer for easy comparison
};
unsigned int length; // Length of following data in bytes (BIG ENDIAN)
};
midi_chunk_t chunk;
while (!feof(fp))
{
fread(&chunk, sizeof(chunk), 1, fp);
chunk.length = swapbytes(chunk.length);
if (chunk.id == MIDI_TRACK_CHUNK)
{
processTrack(fp, chunk.length);
}
else
{
fseek(fp, chunk.length, SEEK_CUR);
}
}
For some reason, feof() never seems to trigger the end of the loop. When I run it on a file, it correctly reads the chunks that actually exist, and then continues past the end of the file, reading garbage until I press Ctrl+C.
I can, however, correctly detect the end of the file by checking the return value of the fread() call. It returns 0 when there is no more data to be read.
I don't understand why feof() isn't returning true. I compiled this both with MSVC 7.1 and with g++, and the behavior is the same, so I figure it's a problem with my code and not the compiler or the libraries. Any ideas?












