failbit is being set and I can't figure out why

Posted by felipedrl on Game Development See other posts from Game Development or by felipedrl
Published on 2012-09-28T14:02:42Z Indexed on 2012/09/28 15:51 UTC
Read the original article Hit count: 239

Filed under:

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(false);
    }
    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);
}

© Game Development or respective owner

Related posts about c++