Hi !
I had tried, but honestly, I belive that I had trouble because in is not so wise to use AnsiString in a struct. I have no ide.
BUT !
I had started from the begginning, and I had reconstruct the code. I post here a working version, which also include a little bit of TreeView manipulation, but the main point, that the struct save and load operating perfectly.
BTW : Can I count on the fact that in any windows environment, the absolute maxium for a file or directory name length can not exceed 256 ?
Finnaly, MANY THANKS for that great tutorial, because this version is also built on that.
The code:
//---------------------------------------------------------------------------
void __fastcall Tmain_form::Button1Click(TObject *Sender)
{
struct DataStruct
{
char name[257];
int level;
int image_index;
int selected_index;
};
DataStruct item_data;
TTreeNode * current_item;
ofstream data_file;
data_file.open("c:\\saved.dat", ios::binary | ios::trunc);
for ( int a = 0; a < main_form->main_tree->Items->Count; a++ )
{
current_item = main_form->main_tree->Items->Item[a];
strcpy(item_data.name, current_item->Text.c_str());
item_data.level = current_item->Level;
item_data.image_index = current_item->ImageIndex;
item_data.selected_index = current_item->SelectedIndex;
data_file.write((unsigned char*)&item_data , sizeof(DataStruct));
}
data_file.close();
}
//---------------------------------------------------------------------------
void __fastcall Tmain_form::Button2Click(TObject *Sender)
{
main_form->main_tree->Items->Clear();
}
//---------------------------------------------------------------------------
void __fastcall Tmain_form::Button3Click(TObject *Sender)
{
struct DataStruct
{
char name[257];
int level;
int image_index;
int selected_index;
};
DataStruct item_data;
int total_size;
int total_nr_items;
ifstream data_file("c:\\saved.dat",ios::binary | ios::in);
data_file.seekg(0 , ios::end);
total_size = data_file.tellg();
total_nr_items = total_size / sizeof(DataStruct);
for (int a = 0; a < total_nr_items; a++)
{
data_file.seekg(a * sizeof(DataStruct),ios::beg);
data_file.read((char*)&item_data , sizeof(DataStruct));
Memo1->Lines->Add(item_data.name);
}
data_file.close();
}
//---------------------------------------------------------------------------
Moore