This message was edited by Phat Nat at 2005-5-31 17:36:12
: I'm wanting to create an account file that holds account information, eg:
:
: on my atm program it is possible to create a new account. What I don't know is how do I keep adding new accounts to this file?
:
: Stooboy
:
If your account is stored as a record variable, it is quite easy. You can write the record to a file and then use the
Append() procedure to add on to a file if done in
Text mode or
Reset() if done in
File Mode (preferable). The only problem you'll have is if you need to delete a record, but even this is quite easy.
You can either have an array of your records or just one and read records from the file one-by-one until you find the one you want. The second way allows a huge size of records without trying to use pointers, etc.
If you want to delete a record the second method, you would have to copy your data file to another temp file, then copy it back over the original one record at a time and just don't copy the record you want to delete. Then delete the Temp file.
TYPE
MyRecord = record
Name : String;
Age : Byte;
{...}
END;
VAR
F : File;
Data : MyRecord;
X : Byte;
Begin
Assign(F,'MYFILE.DAT'); {$I-}
Reset(F,1);
If IOResult <> 0 Then Rewrite(F,1);
X := 0;
While Not(Eof(F)) Do
Begin
Inc(X);
BlockRead(F,MyRecord,SizeOf(MyRecord));
WriteLn('Record #',X);
WriteLn(' * Name = ',Data.Name);
WriteLn(' * Age = ',Data.Age);
WriteLn;
While Keypressed Do Readkey;
Readkey;
End;
WriteLn('Enter Data:');
Write('Name = ');
ReadLn(Data.Name);
If Data.Name <> '' then
Begin
WriteLn('Age = ');
ReadLn(Data.Age);
Seek(F,FileSize(F));
BlockWrite(F,MyRecord,SizeOf(MyRecord));
End;
End.
Here's an example of storing & Reading records to a file.
Phat Nat