CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jan 2009
    Posts
    399

    Read bytes from CFile buffer

    How to read correctly the CFile buffer from a certain location ?

    Here is the code (simplified):
    Code:
    BOOL CMyDoc::SaveFileModified(CImg<float>& img, const CString sFileName)
    {
    	CString sDrive, sDir;
    	_splitpath(sFileName, sDrive.GetBuffer(), sDir.GetBuffer(), NULL, NULL);
    
    	CString sTemp;
    	sTemp.Format(_T("%s%sTemp.tmp"), sDrive, sDir);
    
    	sDrive.ReleaseBuffer();
    	sDir.ReleaseBuffer();
    
    	img.get_invert_endianness().save_exr(sTemp);
    
    	CFile fileTemp;
    	if (! fileTemp.Open(sTemp, CFile::typeBinary | CFile::modeRead))
    		return FALSE;
    
    	char c;
    	UINT nRead = 0;
    	int nCount = 0;
    	ULONGLONG nPosition = 0;
    	CByteArray arrHeader, arrData;
    	do
    	{
    		nRead = fileTemp.Read((char*)&c, sizeof(char));
    		if (nCount < 20)
    		{
    			arrHeader.Add(static_cast<BYTE>(c));
    		}
    		nCount++;
    		if (nCount >= 20)    // read the header, so go out
    		{
    			nPosition = fileTemp.GetPosition();
    			break;
    		}
    	}while (nRead > 0);
    
    	if (nPosition > 1)  // try to read the rest of the file from nPosition to end of the file
    	{
    		arrData.SetSize(fileTemp.GetLength() - nPosition);
    		fileTemp.Read(arrData.GetData(), (UINT)arrData.GetSize());
    	}
    
    	fileTemp.Close();
    
    	CFile file;
    	if (! file.Open(sFileName, CFile::typeBinary | CFile::modeCreate | CFile::modeReadWrite))
    		return FALSE;
    
    	arrHeader.Append(arrData);
    	file.Write(arrHeader.GetData(), (UINT)arrHeader.GetSize());
    	file.Close();
    
    	return TRUE;
    }
    the result stored in sFileName is incorrect ... to prove the result I should give more details and the example would be a little complex ... but if is necessary I put the real example here. With this code, I have done something wrong ? Because I am struggle to spot a wrong, but I can't see anything ...

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Read bytes from CFile buffer

    the result stored in sFileName is incorrect
    In what way?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Jan 2009
    Posts
    399

    Re: Read bytes from CFile buffer

    the sfileName is an image. And the resulted image is darker. ... Hmmm. I guess I should give the entire code, should I ?

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Read bytes from CFile buffer

    Quote Originally Posted by mesajflaviu View Post
    Hmmm. I guess I should give the entire code, should I ?
    Naw, we like to work for our answers. <wink>

  5. #5
    Join Date
    Jan 2009
    Posts
    399

    Re: Read bytes from CFile buffer

    I see.

    Ok. The task is to open the file, which is image, and this image has serialized 3 strings at the beginning, and the rest of the file is the image itself (bytes only with image graphic). I have to modify string 3, and save it back.
    Code:
    BOOL CMyDoc::SaveFileModified(CImg<float>& img, const CString sFileName)
    {
    	CString sDrive, sDir;
    	_splitpath(sFileName, sDrive.GetBuffer(), sDir.GetBuffer(), NULL, NULL);
    
    	CString sTemp;
    	sTemp.Format(_T("%s%sTemp.tmp"), sDrive, sDir);
    
    	sDrive.ReleaseBuffer();
    	sDir.ReleaseBuffer();
    
    	img.get_invert_endianness().save_exr(sTemp);
    
    	CFile fileTemp;
    	if (! fileTemp.Open(sTemp, CFile::typeBinary | CFile::modeRead))
    		return FALSE;
    
    	char c;
    	UINT nRead = 0;
    	int nCount = 0;
    	CString s0, s2, s2;
    	ULONGLONG nPosition = 0;
    
    	do
    	{
    		nRead = fileTemp.Read((char*)&c, sizeof(char));
    		switch (nCount)
    		{
    		case 0:
    			s0 += c;
    			break;
    		case 1:
    			s1 += c;
    			break;
    		case 2:
    			s2 += c;
    			break;
    		}
    		if ('\n' == c)		// is new line
    		{
    			nCount++;
    		}
    		if (nCount >= 3)	// read the header, so go out
    		{
    			nPosition = fileTemp.GetPosition();
    			break;
    		}
    	}while (nRead > 0);
    
    	CByteArray arrData;
    	if (nPosition > 1)	// read the rest of the file into arrData
    	{
    		arrData.SetSize(fileTemp.GetLength() - nPosition);
    		fileTemp.Read(arrData.GetData(), (UINT)arrData.GetSize());
    	}
    
    	fileTemp.Close();
    
    	CFile file;
    	if (! file.Open(sFileName, CFile::typeBinary | CFile::modeCreate | CFile::modeReadWrite))
    		return FALSE;
    
    	// save back sFileName with s2 modified
    	s2 = _T("abc");
    
    	CByteArray arrTemp;
    
    	ConvertCStringToByteArray(s0, arrTemp);
    	arrHeader.Append(arrTemp);
    	arrTemp.RemoveAll();
    	ConvertCStringToByteArray(s1, arrTemp);
    	arrHeader.Append(arrTemp);
    	arrTemp.RemoveAll();
    	ConvertCStringToByteArray(s2, arrTemp);
    	arrHeader.Append(arrTemp);
    
    	arrHeader.Append(arrData);
    	file.Write(arrHeader.GetData(), (UINT)arrHeader.GetSize());
    	file.Close();
    
    	return TRUE;
    }
    code seem to be simple and clear, still, the new saved image are darker.
    Last edited by mesajflaviu; December 12th, 2019 at 02:31 AM.

  6. #6
    Join Date
    Jan 2009
    Posts
    399

    Re: Read bytes from CFile buffer

    Code:
    INT_PTR CMyDoc::ConvertCStringToByteArray(const CString& sValue, CByteArray& arrData)
    {
    	BYTE* pByteArray = (PBYTE)(LPCTSTR)sValue;
    	arrData.SetSize(sValue.GetLength());
    	memcpy(arrData.GetData(), pByteArray, sValue.GetLength());
    
    	return arrData.GetSize();
    }

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Read bytes from CFile buffer

    Can you save the second image to a file and compare with the original file to see if there is a difference in the file bytes?

  8. #8
    Join Date
    Jan 2009
    Posts
    399

    Re: Read bytes from CFile buffer

    Temp.tmp is the same with sFileName (I have checked byte with byte), but sFileName is not the same with the previous one because the second one is resulting of "get_invert_endianness" operation.

  9. #9
    Join Date
    Jan 2009
    Posts
    399

    Re: Read bytes from CFile buffer

    Due to mode to test the file image result, I thought that the code was wrong, but seem to be ok.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured