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 ...
Re: Read bytes from CFile buffer
Quote:
the result stored in sFileName is incorrect
In what way?
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 ?
Re: Read bytes from CFile buffer
Quote:
Originally Posted by
mesajflaviu
Hmmm. I guess I should give the entire code, should I ?
Naw, we like to work for our answers. <wink>
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.
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();
}
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?
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.
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.