Board index » delphi » File and Record

File and Record


2004-06-24 09:50:05 PM
delphi102
Hello everyone,
I'm trying to create small text database file for about 100 records only
I want to be able to add, edit, delete and find from same file.
I spent hours to find out from help file(which needs help) and websites, but
I couldn't
find any kind solution for what I am looking for.
any help or guide will appreciated, thanks in advance
I have this
Type
TCustomer = Record
RecordID : string[7];
firstName : string[20];
lastName : string[20];
address1 : string[100];
address2 : string[100];
city : string[20];
postCode : string[8];
End;
//Not working always overwrite my record
procedure TForm1.AddNewClick(Sender: TObject);
var
F : file of TCustomer;
MyCustomer : TCustomer;
begin
MyCustomer.RecordID :='0000123';
MyCustomer.firstName :='Mike';
MyCustomer.lastName :='Test';
MyCustomer.address1 :='123 Street';
MyCustomer.address2 :='No 121';
MyCustomer.city :='Anycity';
MyCustomer.postCode :='123456';
//add record
AssignFile(F,'C:\MyCustomers.dat');
// Rewrite(F); // overwrite the file-not good
Reset(F);
try
Write(F, MyCustomer );
finally
CloseFile(F);
end;
end;
 
 

Re:File and Record

Mike Moore writes:
Quote
I'm trying to create small text database file for about 100 records
only I want to be able to add, edit, delete and find from same file.
I spent hours to find out from help file(which needs help) and
websites, but I couldn't
find any kind solution for what I am looking for.
any help or guide will appreciated, thanks in advance
It's possible to do this using a stream, but *much easier* to use a
file-based dataset such as a TClientDataset.
-Craig
--
Craig Stuntz [TeamB] ?Vertex Systems Corp. ?Columbus, OH
Delphi/InterBase Weblog : blogs.teamb.com/craigstuntz
Everything You Need to Know About InterBase Character Sets:
blogs.teamb.com/craigstuntz/articles/403.aspx
 

Re:File and Record

In article <40dadc54$XXXX@XXXXX.COM>, "Mike Moore" <Mike77 at
mywebmail dot com>writes:
Quote
//Not working always overwrite my record
//add record
AssignFile(F,'C:\MyCustomers.dat');
// Rewrite(F); // overwrite the file-not good
Reset(F);
try
Write(F, MyCustomer );
You just opened the file so it is at the first record. So it overwrites the
first record, and you never get as far as writing a second record.
Ways to get to the end of the file (look for help on these) are
READ (until EOF)
SEEK
To find a record Read(F,TheRec) until eof(f) or
(TheRec.RecordID=MyCustomer.RecordID)
if not eof, you can then edit by seeking back 1 record and writing.
To delete, you must either include a flag in your record to mark it as
deleted, or maybe use a value for ID that will never be given as a real ID..
or copy the entire file to a new file omitting the deleted record.
.. Or since the data is small do all your work with an array of records.
Read the file into the array (Blockread) at the beginning and write it back
to the file at the end.
 

Re:File and Record

If you want to do it in the "old-Pascal style", here is some
example:
------ 8< ------- beginning of example ---------
Type MyRecord = // Your record
record
Something: Integer;
IsValid: Boolean; // Set to False to "delete".
end;
Var MyArray: Array[1..MaxSize] of MyRecord;
StoredIndex: Integer;
Procedure LoadFromFile(filename:String);
var hlpFile: File of MyRecord;
MyIndex : Integer;
begin
AssignFile(hlpFile,filename);
//You should make sure the filename is valid!
Reset(hlpFile);
MyIndex := 1;
While not eof(hlpFile) do
begin
Read(hlpFile,MyArray[myindex]);
MyArray[MyIndex].IsValid := True;
Inc(MyIndex);
end;
//Now MyIndex = index BEHIND the last record, store
// this to be able to add easily:
StoredIndex := MyIndex;
CloseFile(hlpFile);
end;
Procedure SaveToFile(filename:String);
var hlpFile: File of MyRecord;
MyIndex : Integer;
begin
AssignFile(hlpFile,filename);
//You should make sure the filename is valid!
Rewrite(hlpFile);
For myIndex := Low(MyArray) to High(MyArray) do
begin
if (MyArray[MyIndex].IsValid) then
Write(hlpFile,MyArray[myindex]);
end;
CloseFile(hlpFile);
end;
Procedure AddToDatabase(newSomething:Integer);
begin
MyArray[StoredIndex].Something := newSomething;
MyArray[StoredIndex].IsValid := True;
Inc(StoredIndex);
end;
Procedure DeleteAtIndex(IndexToDelete:Integer);
begin
MyArray[IndexToDelete].IsValid := False;
end;
------8<--- end of example -----------
Notes:
1) This is _very_ slavish way of coding, try using some "deluxe" stuff that
Delphi offer (Datasets etc.). But if you actually manage to do it this way,
you will at least have very good understanding of the difference between
database in memory and database in file.
2) The code _could_ use a lot of facelifting, but what the heck :-)
-- Pavel Vymazal