hi,
I am hoping someone else has run into this problem before. I have a bitmap loader as part of my project and I originally wrote it all in ansci C, but had to make the file I/O MFC because the fread function was incorrectly hitting the eof for certain bitmaps, and CFile's Read function works correctly. I've included a snippet of the code, where the C code is commented out to make way for the C++ code. The fread call where the *** is reads only 1 byte of data and says its at the end of the file, where the Read call reads the many bytes that are left, which is correct.
Has anyone seen this before and do you know how to get around it??
Derek
Code:
// FILE* pFile = fopen(filename, "r");
CFile file(filename, CFile::modeRead);
// if (!pFile)
// return false;
// read header
// if (fread(&fileHeader, 1, sizeof(BITMAPFILEHEADER), pFile) != sizeof(BITMAPFILEHEADER))
if (file.Read(&fileHeader, sizeof(BITMAPFILEHEADER)) != sizeof(BITMAPFILEHEADER))
{
// fclose(pFile);
return false;
}
if (((char *)&fileHeader.bfType)[0] != 'B' || ((char *)&fileHeader.bfType)[1] != 'M')
{
// fclose(pFile);
return false;
}
// read the rest of the file...
// start by getting the file size
struct _stat buf;
if (_stat(filename, &buf) != 0)
{
// fclose(pFile);
return false;
}
unsigned int fileSize = buf.st_size - sizeof(BITMAPFILEHEADER);
if (m_pFileData)
delete [] m_pFileData;
m_pFileData = new BYTE[fileSize];
// ***
// if (int s = fread(m_pFileData, 1, fileSize, pFile) != fileSize)
if (file.Read(m_pFileData, fileSize) != fileSize)
{
delete [] m_pFileData;
m_pFileData = NULL;
// fclose(pFile);
return false;
}
// close the file, we're done with it
// fclose(pFile);