|
-
December 30th, 2001, 03:06 PM
#1
Storing 32-bit values like DWORD
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.
-
December 30th, 2001, 03:43 PM
#2
Re: Storing 32-bit values like DWORD
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
-
December 30th, 2001, 03:53 PM
#3
Re: Storing 32-bit values like DWORD
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: iskFull)
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
-
December 30th, 2001, 05:02 PM
#4
Re: Storing 32-bit values like DWORD
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.
-
December 30th, 2001, 06:35 PM
#5
Re: Storing 32-bit values like DWORD
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: iskFull)
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|