I have created a demo form with finish, save, load, add, and show
buttons and four edit boxes Edit1 through Edit4
Here is the code pasted from my main (and only) form to do what I think
you want. Depending on what version CB you are running you can update
my old stdio.h type commands to newer <iostream.h> type commands ( or
get back to me for translation) but this should give the gist of what's
needed. . No error checking in code so save you working out from
listing. put name in edit1 ints in 2 and 3 click add repeat etc put
value 0 to max entries-1 in edit4 click show . save before load
#include <vcl.h>
#pragma hdrstop
#include "main.h"
#include <string.h> //
//#include <iostream> //
#include <stdio.h>
#include <dstring.h>// iostream must be before dstream to use stream
operators
#include <dir.h> // on AnsiStrings
#pragma package(smart_init)
#pragma resource "*.dfm"
TMainForm *MainForm;
//I have set up following globally (ugh!) you can place as appropriate
to your app
//****************************************************************
#define MAXFILEPATH 100
typedef struct
{
char name[50];
int BP;
int WP;
}TSettings;
typedef TSettings* LPointer;
LPointer TheStorageStructure;
TList *DemoList = new TList;
//*****************************************************************
//-----------------------------------------------------------------------
----
__fastcall TMainForm::TMainForm(TComponent* Owner)
: TForm(Owner)
{
}
//-----------------------------------------------------------------------
----
void __fastcall TMainForm::finishClick(TObject *Sender)
{
for(int n=0; n<DemoList->Count; n++)
{
TheStorageStructure = (LPointer) DemoList->Items[n];
delete TheStorageStructure;
}
delete DemoList;
Application->Terminate();
}
//-----------------------------------------------------------------------
----
void __fastcall TMainForm::saveClick(TObject *Sender)
{
//save the structure data
AnsiString FilerRoot, FileName; // FilerRoot to hold current working
directory - cwd
int n=0; // index of structure being
processed
FILE* fh=0; // fh var to hold file handle,
char ThisPath[MAXFILEPATH]; // buffer for getcwd()
getcwd(ThisPath,MAXFILEPATH); // in dir.h
FilerRoot=ThisPath;
FileName=FilerRoot+"\\tlistdemo.jke"; // use whatever file extension
you feel appropriate
n=-1;
try
{
fh=fopen(FileName.c_str(),"w+b"); // create empty file for update
binary
while(n<((DemoList->Count)-1)) // < also w-1 as count is
number of items 1 2 3 etc but array zero
// indexed and n incremented
inside loop
{
n++;
TheStorageStructure = (LPointer) DemoList->Items[n];
fseek(fh,(long)(sizeof(TSettings)*n),SEEK_SET);
fwrite(TheStorageStructure,sizeof(TSettings),1,fh);
}
fclose(fh);
}
catch(...)
{
fcloseall();
}
}
//-----------------------------------------------------------------------
----
void __fastcall TMainForm::loadClick(TObject *Sender)
{
//reload the structure data
AnsiString FilerRoot, FileName; //FilerRoot to hold current working
directory - cwd
FILE* fh=0; // fh var to hold file handle,
int n=0;
char ThisPath[MAXFILEPATH];
getcwd(ThisPath,MAXFILEPATH);
FilerRoot=ThisPath;
FileName=FilerRoot+"\\tlistdemo.jke";
n=-1;
try
{
fh=fopen(FileName.c_str(),"r+b"); // re-open empty file for read
write random access
while(feof(fh)==0)
{
n++;
TheStorageStructure = new TSettings;
fseek(fh,(long)(sizeof(TSettings)*n),SEEK_SET);
fread(TheStorageStructure,sizeof(TSettings),1,fh);
DemoList->Add(TheStorageStructure);
}
fclose(fh);
}
catch(...)
{
fcloseall();
}
}
//-----------------------------------------------------------------------
----
void __fastcall TMainForm::addClick(TObject *Sender)
{
//add some data into a 'structure unit'
TheStorageStructure = new TSettings;
strcpy(TheStorageStructure->name,Edit1->Text.c_str());
TheStorageStructure->BP=atoi(Edit2->Text.c_str());
TheStorageStructure->WP=atoi(Edit3->Text.c_str());
DemoList->Add(TheStorageStructure);
Edit4->Text=DemoList->Count; //display current item count added
}
//-----------------------------------------------------------------------
----
void __fastcall TMainForm::showClick(TObject *Sender)
{
//show an item - type required index in Edit4 remember to count
from zero - no error checking!
TheStorageStructure = (LPointer)
DemoList->Items[atoi(Edit4->Text.c_str())];
Edit1->Text= TheStorageStructure->name;
Edit2->Text= TheStorageStructure->BP;
Edit3->Text= TheStorageStructure->WP;
}
--
John Kettle