I tried different ways to load file contents in to structs but have some trouble. Simply because the structs contain strings, which makes them variable in size. The file is supposed to hold several instanses which each needs to be loaded in to a struct.
I am thinking that the file header contains the number of structs in the file. The struct header can contain the length of the string.
Would you be so kind to show me how I can read a chunk of data from a file and cast that in to a struct that has a string in it? (btw are std::strings bad for this?)
LOADSPRITEOBJECT LoadSpriteStruct;
std::ifstream inbal("spritedata.txt", std::ios::in | std::ios::binary);
if(!inbal) {
MessageBox(NULL, L"Unable to open", L"Error", MB_OK);
PostQuitMessage(0);
return;
}
inbal.read((char *) &LoadSpriteStruct, sizeof(LoadSpriteStruct));
//inbal.close();
MakeSprite(LoadSpriteStruct); //init sprite and push in to a vector
void USERINTERFACEBOX::MakeSprite(LOADSPRITEOBJECT &instance)
{
SPRITEOBJECT Sprite;
//Set read data
Sprite.Color = LoadSpriteStruct.Color;
Sprite.name = LoadSpriteStruct.name;
Sprite.OffsetPosition = LoadSpriteStruct.OffsetPosition;
Sprite.visible = LoadSpriteStruct.visible;
//Calculate position relative parent box
Sprite.position = BoxPosition + Sprite.OffsetPosition;
//Init Sprite and put in array
SpriteHandler.LoadSprite(Sprite);
Sprites.push_back(Sprite);
struct LOADSPRITEOBJECT
{
int NameLength;
std::string name;
D3DXVECTOR3 OffsetPosition;
D3DCOLOR Color;
bool visible;
};