User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 392,088 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,938 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 4602 | Replies: 4
Reply
Join Date: Jul 2004
Posts: 9
Reputation: coolice is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
coolice coolice is offline Offline
Newbie Poster

Help fstream and struct question

  #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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2004
Posts: 250
Reputation: FireNet will become famous soon enough FireNet will become famous soon enough 
Rep Power: 6
Solved Threads: 6
FireNet's Avatar
FireNet FireNet is offline Offline
Posting Whiz in Training

Re: fstream and struct question

  #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  
Join Date: Jul 2004
Posts: 9
Reputation: coolice is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
coolice coolice is offline Offline
Newbie Poster

Re: fstream and struct question

  #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  
Join Date: May 2004
Posts: 250
Reputation: FireNet will become famous soon enough FireNet will become famous soon enough 
Rep Power: 6
Solved Threads: 6
FireNet's Avatar
FireNet FireNet is offline Offline
Posting Whiz in Training

Re: fstream and struct question

  #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  
Join Date: Apr 2004
Posts: 3,450
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 16
Solved Threads: 138
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: fstream and struct question

  #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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 12:38 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC