fstream and struct question

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2004
Posts: 9
Reputation: coolice is an unknown quantity at this point 
Solved Threads: 0
coolice coolice is offline Offline
Newbie Poster

fstream and struct question

 
0
  #1
Jul 24th, 2004
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
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 256
Reputation: FireNet will become famous soon enough FireNet will become famous soon enough 
Solved Threads: 6
FireNet's Avatar
FireNet FireNet is offline Offline
Posting Whiz in Training

Re: fstream and struct question

 
0
  #2
Jul 24th, 2004
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.
See what you can, remember what you need

Fourzon | Earn via Coding
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 9
Reputation: coolice is an unknown quantity at this point 
Solved Threads: 0
coolice coolice is offline Offline
Newbie Poster

Re: fstream and struct question

 
0
  #3
Jul 24th, 2004
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
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 256
Reputation: FireNet will become famous soon enough FireNet will become famous soon enough 
Solved Threads: 6
FireNet's Avatar
FireNet FireNet is offline Offline
Posting Whiz in Training

Re: fstream and struct question

 
0
  #4
Jul 25th, 2004
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.
See what you can, remember what you need

Fourzon | Earn via Coding
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,335
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 236
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: fstream and struct question

 
0
  #5
Jul 25th, 2004
Originally Posted by coolice
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.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC