|
-
May 29th, 2009, 11:17 AM
#1
Loading Binary Data from a file into a Structure
I am lost here, I know I can use a bunch of arrays to do this, but I have the structures that I use in Hex Workshop, and would like to use them here.
My problem is that I do not understand exactly how structures work in this type of a situation. I have Structures nested within Structures, nested within Structures. See below (these were indeed created by someone else, I merely edited the data types to work with C++, not just Hex Workshop, and yes, it is for a save state editor)
struct Text
{
unsigned __int16 chara[16]; //2-Byte Letter
};
struct Specialist
{
unsigned __int16 Level;
unsigned __int8 Job;
unsigned __int8 Unknown; //Probably determines monster avatar
};
struct Item
{
struct Specialist Specialists[16];
unsigned __int64 EXP;
unsigned __int32 Unknown; //Unknown at this time
unsigned __int32 Unknown; //Unknown at this time
unsigned __int32 HP;
unsigned __int32 SP;
unsigned __int32 Atk;
unsigned __int32 Def;
unsigned __int32 Int;
unsigned __int32 Spd;
unsigned __int32 Hit;
unsigned __int32 Res;
unsigned __int16 HP_base;
unsigned __int16 SP_base;
unsigned __int16 Atk_base;
unsigned __int16 Def_base;
unsigned __int16 Int_base;
unsigned __int16 Spd_base;
unsigned __int16 Hit_base;
unsigned __int16 Res_base;
unsigned __int16 Type;
unsigned __int16 Level;
unsigned __int32 Unknown;
unsigned __int8 Rarity;
unsigned __int8 Unknown;
unsigned __int8 Unknown;
unsigned __int8 SpecialistSlots;
unsigned __int16 Unknown;
unsigned __int8 Unknown;
unsigned __int8 Unknown;
unsigned __int64 Unknown;
unsigned __int8 Unknown;
unsigned __int8 Unknown;
unsigned __int8 Unknown;
struct Text Name;
unsigned __int8 Unknown[13];
};
struct StoredItems
{
struct Item ItemBag[16];
struct Item Warehouse1[128];
struct Item Warehouse2[128];
};
struct Character
{
unsigned __int64 EXP;
struct Item Weapon;
struct Item Etc1;
struct Item Etc2;
struct Item Etc3;
struct Text Name;
unsigned __int16 Unknown;
struct Text Class;
unsigned __int16 Unknown;
unsigned __int8 Unknown[34];
unsigned __int16 PoisonResistance;
unsigned __int16 SleepResistance;
unsigned __int16 DepraveResistance;
unsigned __int16 ForgetResistance;
unsigned __int16 ParalyzeResistance;
unsigned __int8 Unknown[112];
unsigned __int32 SkillEXP[96];
unsigned __int16 Skills[96];
unsigned __int8 SkillLevel[96];
unsigned __int32 HP_current;
unsigned __int32 SP_current;
unsigned __int32 HP;
unsigned __int32 SP;
unsigned __int32 Atk;
unsigned __int32 Def;
unsigned __int32 Int;
unsigned __int32 Spd;
unsigned __int32 Hit;
unsigned __int32 Res;
unsigned __int32 HP_actual;
unsigned __int32 SP_actual;
unsigned __int32 Atk_actual;
unsigned __int32 Def_actual;
unsigned __int32 Hit_actual;
unsigned __int32 Spd_actual;
unsigned __int32 Int_actual;
unsigned __int32 Res_actual;
unsigned __int8 Unknown[32];
unsigned __int32 Mana;
unsigned __int32 Unknown;
unsigned __int8 Unknown[12];
unsigned __int8 Unknown[24];
unsigned __int8 HP_base;
unsigned __int8 SP_base;
unsigned __int8 Atk_base;
unsigned __int8 Def_base;
unsigned __int8 Int_base;
unsigned __int8 Spd_base;
unsigned __int8 Hit_base;
unsigned __int8 Res_base;
unsigned __int8 Unknown[36];
};
void loadstate() { //Attached to the Load menu item
// Load characters
for (int i = 1; i <= 27; i++){
2984 + 1992 * (i - 1) //Need Offset Navigation Code here
Character chara[i]{
in_file.read (chara[i].EXP,4)
in_file.read (chara[i].Weapon)
}
}
As you can see the size is already defined within these structures. I have already found and implemented the code that allows me to load a file, and then navigate to a specific offset, and also shoved the stepping of the offset into the array, as there can be 27 characters in this game.
What eludes me, is how do I load the data into the structures? If I use the in_file.read (structure location, data length), do I really need to set the length, can't I just let the structure location read what it needs? Should I just continue to type out each and every location, or is there another way? How does nested structures work in my situation? Am I going in the completely wrong direction? The whole structure is of a specific length, in which after the last entry of the character structure, the next character structure begins.
This will be my first foray into C++, if you couldn't guess, I am not looking for someone to write the coding for me, I am merely looking for pointers on how I can proceed. I have searched every combination of terms I can find, with no luck. Any feedback, tips, directions, will be greatly appreciated.
-
May 29th, 2009, 01:27 PM
#2
Re: Loading Binary Data from a file into a Structure
If padding of the C++ structure and the file content layout match, you just need to do something like this:
Code:
TopLevelStruct oStruct;
in_file.read(&oStruct,sizeof(TopLevelStruct));
You won't need to read byte by byte and update individual members that way
-
May 29th, 2009, 03:31 PM
#3
Re: Loading Binary Data from a file into a Structure
Thank you!! Does that take into account nested structures?
-
May 29th, 2009, 03:33 PM
#4
Re: Loading Binary Data from a file into a Structure
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
|