Click to See Complete Forum and Search --> : Storing 32-bit values like DWORD
RandyG
December 30th, 2001, 02:06 PM
I'm using a CFile and want to write out a DWORD (or a UINT). DWORD is 32-bits, so that's 4 bytes.
When I do a CFile::Write(&dwordVar, 4) it's not producing what I expect.
How do I write a DWORD/UINT properly?
Thanks.
Andreas Masur
December 30th, 2001, 02:43 PM
It looks okay...what kind of error do you have?? The only thing I would change is the hardcoded '4' for the length...
CFile File;
CFileException Exception;
// Open etc.
if(File.Open(_T("c:\\test.txt"), CFile::modeCreate | CFile::modeWrite, &Exception) == FALSE)
{
// Could not open file
TCHAR szErrorMessage[1024];
pEx->GetErrorMessage(szErrorMessage, 1024);
AfxMessageBox(szErrorMessage, "Error");
pEx->Delete();
}
else
{
// Write to file
try
{
CFile::Write(static_cast<const void *>(&dwordVar), sizeof(dwordVar));
}
catch(CFileException *pEx)
{
TCHAR szErrorMessage[1024];
pEx->GetErrorMessage(szErrorMessage, 1024);
AfxMessageBox(szErrorMessage, "Error");
pEx->Delete();
}
}
Ciao, Andreas
"Software is like sex, it's better when it's free." - Linus Torvalds
Andreas Masur
December 30th, 2001, 02:53 PM
Ooppss slight modification...(I hate copy and paste) ;-)
CFile File;
CFileException Exception;
// Open etc.
if(File.Open(_T("c:\\test.txt"), CFile::modeCreate | CFile::modeWrite, &Exception) == FALSE)
{
// Could not open file
TCHAR szErrorMessage[1024];
Exception.GetErrorMessage(szErrorMessage, 1024);
AfxMessageBox(szErrorMessage, "Error");
Exception.Delete();
}
else
{
// Write to file
try
{
CFile::Write(static_cast<const void *>(&dwordVar), sizeof(dwordVar));
}
catch(CFileException *pEx)
{
if(pEx->m_cause == CFileException::diskFull)
AfxMessageBox("Disk is full", "Error");
else
{
TCHAR szErrorMessage[1024];
pEx->GetErrorMessage(szErrorMessage, 1024);
AfxMessageBox(szErrorMessage, "Error");
}
pEx->Delete();
}
}
Ciao, Andreas
"Software is like sex, it's better when it's free." - Linus Torvalds
RandyG
December 30th, 2001, 04:02 PM
Okay I should of been more clear, oops.
If I use your code I get the same problem, but better error protection that I had before :)
The problem is that the byte order is written in reverse from what I need it to be in. So writing 0xAABBCCDD results in the file containing ...DD CC BB AA...(EOF) , when I need it to be AA BB CC DD. Is there a simple way to fix that?
Thanks.
Andreas Masur
December 30th, 2001, 05:35 PM
Why do you need to fix it?? I mean, don't get me wrong but if you read in the value again with the 'Read()' function you will get the same value so I don't see the problem...
DWORD dwValue = 25;
CFile File;
CFileException Exception;
// Open file
if(File.Open(_T("c:\\test.txt"), CFile::modeCreate | CFile::modeWrite, &Exception) == FALSE)
{
// Could not open file
TCHAR szErrorMessage[1024];
Exception.GetErrorMessage(szErrorMessage, 1024);
AfxMessageBox(szErrorMessage);
Exception.Delete();
}
else
{
// Write to file
try
{
File.Write(static_cast<const void *>(&dwValue), sizeof(dwValue));
}
catch(CFileException *pEx)
{
if(pEx->m_cause == CFileException::diskFull)
AfxMessageBox("Disk is full");
else
{
TCHAR szErrorMessage[1024];
pEx->GetErrorMessage(szErrorMessage, 1024);
AfxMessageBox(szErrorMessage);
}
pEx->Delete();
}
}
// Close file
File.Close();
// Open file again
if(File.Open(_T("c:\\test.txt"), CFile::modeRead, &Exception) == FALSE)
{
// Could not open file
TCHAR szErrorMessage[1024];
Exception.GetErrorMessage(szErrorMessage, 1024);
AfxMessageBox(szErrorMessage);
Exception.Delete();
}
else
{
// Read from file
try
{
File.Read(static_cast<void *>(&dwValue), sizeof(dwValue));
}
catch(CFileException *pEx)
{
TCHAR szErrorMessage[1024];
pEx->GetErrorMessage(szErrorMessage, 1024);
AfxMessageBox(szErrorMessage);
pEx->Delete();
}
}
// Close file
File.Close()
Try this code. If you read in the value again it will be '25'...
Ciao, Andreas
"Software is like sex, it's better when it's free." - Linus Torvalds
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.