Thread: File I/O: CFile Read function versus fread...tough question

  1. #1
    dkrocks
    Guest

    Question File I/O: CFile Read function versus fread...tough question

    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);

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    fread returns the number of items read. Not the number of bytes. Thus, it should return 1.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    dkrocks
    Guest
    yes, I'm aware of that. That's why I switched my size and count parameters in my fread function. ie:

    fread(m_pFileData, 1, fileSize, pFile)
    instead of
    fread(m_pFileData, fileSize, 1, pFile)

    This way it is reading '1' byte 'fileSize' number of times.

    By the way, I forgot to mention that there is only 1 bitmap that it doesn't work for...so I'm starting to think that the eof token must be part of the bitmap data for this bitmap.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by dkrocks
    By the way, I forgot to mention that there is only 1 bitmap that it doesn't work for...so I'm starting to think that the eof token must be part of the bitmap data for this bitmap.
    EOF cannot be included in the middle of the file. It's possible the data for the bitmap is different than the header says it is. For example, there are various headers for .JPG files. Photoshop has a slightly different header than images saved in 'Paint'. Perhaps your header is saying the data should be one way, but the actual data isn't?

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    // FILE* pFile = fopen(filename, "r");

    Here you might try:
    FILE* pFile = fopen(filename, "rb");

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Now that Salem pointed this out:

    > if (((char *)&fileHeader.bfType)[0] != 'B' || ((char *)&fileHeader.bfType)[1] != 'M')

    Instead of ||, shouldn't that be &&:
    if ( fileHeader.bfType[0] != 'B' && fileHeader.bfType[1] != 'M' )

  7. #7
    Waltp
    Guest
    As swoopy mentions, change your open file mode to "rb" to 'read binary'. There is undoubtedtly an EOF value (Crtl-Z) in the file and the mode "r" will stop reading when it hits that character.

    Walt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. How should I read this file (i/o question)
    By mmattax in forum C Programming
    Replies: 13
    Last Post: 01-17-2006, 11:16 AM
  4. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM