Okay, so i am continuing to work on my little game engine to teach me C#/C++ more. Now i am attempting to write a way of storing data in a binary format, that i created. (This is learning, i want to do this my self from scratch). What i am wondering what is the best way of dealing with variable length arrays inside a structure when reading it in C++?
E.g. Here is what i currently have for my structures:
[StructLayout(LayoutKind.Sequential)]
public struct FooBinaryHeader
{
public Int32 m_CheckSumLength;
public byte[] m_Checksum;
public Int32 m_NumberOfRecords;
public FooBinaryRecordHeader[] m_BinaryRecordHeaders;
public FooBinaryRecord[] m_BinaryRecords;
}
[StructLayout(LayoutKind.Sequential)]
public struct FooBinaryRecordHeader
{
public Int32 m_FileNameLength;
public char[] m_FileName;
public Int64 m_Offset;
}
[StructLayout(LayoutKind.Sequential)]
public struct FooBinaryRecord
{
public bool m_IsEncrypted;
public Int64 m_DataSize;
public byte[] m_Data;
}
Now how would i go about in C++ to actually read this in as a structure in C++? I was kinda hoping to get around reading each of the elements one by one and copying them into a structure.
The only real tutorial i found on this is this: http://www.gamedev.net/community/forums/topic.asp?topic_id=310409&whichpage=1�
I’ll take a wild guess and say reading this into a C++ structure is not really possible correct?
You can read it from binary format mapping a copy of these structures. Each array should be treated as a pointer and you should have a integer with size of this array.
For example in
C#
[StructLayout(LayoutKind.Sequential)] public struct A { public Int32 m_CheckSumLength; public byte[] m_Checksum; }C++
struct A { int length char* vector }Notes: byte has the same size of char.
When you read from a binary you can read the first 4 byte (int is 32 aka 4 byte) and allocate 4 + (readed length) after that you can read directly to the allocated buffer and treat as a A structure.