I'm writing a MIDI file loader. Everything is going fine until at some track I get a failbit exception while trying to read from file. I can't figure out why, I've checked the file size and it's ok too.

Upon checking "errno" and it returns "0".

Any ideas? Thanks.

The snippet follows:

file.read(reinterpret_cast<char*>(&mHeader.id), sizeof(MidiHeader));

mTracks = new MidiTrack[mHeader.nTracks];
for (uint i = 0; i < mHeader.nTracks; ++i)
    // this read fails on 6th i. I've checked hexadecimal file and it's
    // ok so far. 
    file.read(reinterpret_cast<char*>(&mTracks[i].id), sizeof(uint));
    if (file.fail())
        std::cerr << errno << std::endl;
    massert(mTracks[i].id == 0x6B72544D);

    file.read(reinterpret_cast<char*>(&mTracks[i].size), sizeof(uint));
    mTracks[i].size = swapBytes(mTracks[i].size);

    mTracks[i].data = new char[mTracks[i].size];
    file.read(mTracks[i].data, mTracks[i].size * sizeof(char));

    totalBytesRead += 8 + mTracks[i].size;
    massert(totalBytesRead <= fileSize);
  • \$\begingroup\$ Is this gamedev related? \$\endgroup\$
    – nathan
    Sep 28, 2012 at 16:07
  • \$\begingroup\$ @nathan As far as I'm concerned games do have sound processing, don't they? and yes, this is for a rhythm game. They usually load guitar and vocal charts as midis. \$\endgroup\$ Sep 28, 2012 at 16:25
  • \$\begingroup\$ For programming questions, see the faq. Specifically, programming questions are on topic if a game programmer would give a better answer than a general programmer. This just seems to be a file loading issue and not game specific. \$\endgroup\$
    – Tetrad
    Sep 28, 2012 at 20:26


Browse other questions tagged .