*Sorry for my bad english and the lack of information.
See below...
On Thu, 06 Nov 2008 13:04:48 +0100, wahid <wahidb@gmail.com> wrote:
Hello,
Im new to MFC i used to program in C# im trying to read a binary file
with this format:
DWORD base_version
DWORD current_version
DWORD vfs_count
:FOREACH( vfs_count )
SSTR vfs_path
DWORD data_offset
:ENDFOR
:FOREACH( vfs_count )
// Seek to <( data_offset )>
DWORD file_count
DWORD unk1
DWORD unk2
:FOREACH file_count
SSTR file_path
DWORD offset
DWORD enc_length
DWORD dec_length
WORD unk3
BYTE unk4
DWORD version
DWORD checksum
:ENDFOR
:ENDFOR
this is the code i use to read it i wanna know if this is the best way
to read it or there is a better way to do it?
if(file.Open(_T(filename), CFile::modeRead, &e))
{
file.Read(&h, sizeof(Header));
for(DWORD i = 0; i < h.vfs_count; i++)
{
file.Read(&VFS.nameLen, sizeof(WORD));
file.Read(&VFS.name, VFS.nameLen);
file.Read(&VFS.data_offset, sizeof(DWORD));
****
THis suggests several things: that the size is a WORD value (16 bits), that the string is
NOT NUL-terminated (otherwise you would need to read VFS.nameLen +1) and there is no
padding added to make the next DWORD lie on a DWORD boundary.
Also, you have not shown us the declaration of VFS, so it is hard to tell what you are
reading into here.
****
lists.Dataoffset.AddTail(VFS.data_offset);
lists.Name.AddTail(VFS.name);
****
You have not given the declaration of lists, nor justified why a list is the best
representation here.
****
}
POSITION pos = lists.Dataoffset.GetHeadPosition();
for(DWORD i = 0; i < h.vfs_count; i++)
{
file.Seek(lists.Dataoffset.GetNext(pos),CFile::begin);
***
The above code does assume that GetNext returns a valid value...
***
file.Read(&Vinfo, sizeof(VFSinfo));
for(DWORD x = 0; x < Vinfo.Filecount; x++)
{
file.Read(&VFS.nameLen, sizeof(WORD));
file.Read(&VFS.name, VFS.nameLen);
file.Read(&VFS.offset, sizeof(DWORD));
file.Read(&VFS.enc_length,sizeof(DWORD));
file.Read(&VFS.dec_length,sizeof(DWORD));
file.Read(&VFS.unk3, sizeof(WORD));
file.Read(&VFS.unk4, sizeof(char));
file.Read(&VFS.version, sizeof(DWORD));
file.Read(&VFS.checksum, sizeof(DWORD));
****
Same caveats as previously; you're sure an SSTR uses a WORD for the length, is not
NUL-terminated, and there is no padding to ensure DWORD-alignment of the next object...
and you have not shown the structure, so it is hard to tell if this code is correct. Note
also that if VFS.name is a character array of fixed size that you have not verified that
the name length is smaller than the array size; alternatively, you have a lot of 64K
strings, which might not be a good idea if there are lots of these structures around.
In asking such a question, it is ESSENTIAL to give ALL the critical information!
****
}
}
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm