Hi !

I would like to ask your help. Since days, I am reading fstream tutorial in tutorials section as I am trying to put together 3routines:

1., Can write a structure to a file. (ButtonClick1)
2., Can tell how many records in the file (ButtonClick2)
3, Can read back the structure from the file. (ButtonClick3)

I thought is simple but I can not get it to work.

- I mean I had created the structure in the main header like this :

private: // User declarations
struct WebSites
{
AnsiString name;
int level;
int image_index;
int selected_index;
};

- And the 3 routines to read and write struct to files :

//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
WebSites p_Data;

p_Data.name = Edit1->Text;
p_Data.level = StrToInt(Edit2->Text);
p_Data.image_index = StrToInt(Edit3->Text);;
p_Data.selected_index = StrToInt(Edit4->Text);;

fstream binary_file("c:\\test.dat",ios::out|ios::text|ios::app);
binary_file.write(reinterpret_cast<char *>(&p_Data),sizeof(WebSites));
binary_file.close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
WebSites p_Data;
int fl_sz;
int total_no_rec;

fstream binary_file("c:\\test.dat",ios::text|ios::in);
binary_file.seekg(0,ios::end);
fl_sz = binary_file.tellg();
total_no_rec = fl_sz/sizeof(WebSites);
Memo1->Lines->Add(total_no_rec);
binary_file.close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
WebSites p_Data;

fstream binary_file("c:\\test.dat",ios::text|ios::in);
binary_file.read(reinterpret_cast<char *>(&p_Data),sizeof(WebSites));
Memo1->Lines->Add(p_Data.name);
binary_file.close();
}
//---------------------------------------------------------------------------

But it is not working consequently. I mean several times cause general error
fault, or not reading the file or it seems the data written is not even
correct.

So I am completely lost. Could somebody help me please ?

Many thanks in advance,

Moore

Recommended Answers

All 4 Replies

Using Standard C++ (98),eh?,Well the tut was in old C++ and I am still learning STL and the new C++ freatures.

reinterpret_cast<char *>(&p_Data): I belive this is the problem area.
Try:reinterpret_cast<char *>(p_Data)

I am not too sure :-| ,so you can even try static_cast<char *>.I really havent got around to using this yet.Better stick to (char*)&p_Data.

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

You are most welcome.I dont know the exactly if there is any directory path size limit but you can be quite sure the almost no one will use a path which is more than a 100 bytes.It's too long.:D

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 ?

I might choose to use the standard macro FILENAME_MAX.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.