: : To save and Read a record, use the following:
: :
: :
: : TYPE
: : Person = record
: : Name : String;
: : Age : Byte;
: : Address : String;
: : End;
: :
: : VAR
: : Bob : Person;
: :
: : PROCEDURE Save(P : Person);
: : VAR
: : F : File;
: : Begin
: : Assign(F,'Save.Dat'); Rewrite(F,1);
: : BlockWrite(F, P, SizeOf(P));
: : Close(F);
: : End;
: :
: : FUNCTION Load(VAR P : Person) : Boolean;
: : VAR
: : NumRead : Word;
: : Temp : Person;
: : Begin
: : {$I-}
: : Assign(F,'Save.Dat'); Reset(F,1);
: : {$I+}
: : If IOResult <> 0 Then
: : Begin
: : WriteLn('File Not Found!);
: : Load := False;
: : Exit;
: : End;
: : BlockRead(F, Temp, SizeOf(Temp),NumRead);
: : Close(F);
: : If NumRead <> SizeOf(Temp) Then
: : Begin
: : WriteLn('File Wrong Size!);
: : Load := False;
: : Exit;
: : End;
: : P := Temp;
: : Load := True;
: : End;
: :
: : Begin
: : If Load(Bob) = False Then
: : Begin
: : Bob.Name := 'Bob';
: : Bob.Age := 33;
: : Bob.Address := '1234 ABC Drive';
: : Save(Bob);
: : WriteLn('Your info has been saved!);
: : End
: : ELSE
: : Begin
: : WriteLn('Your info has been loaded!);
: : WriteLn(' Your name is :',Bob.Name);
: : WriteLn(' Your age is :',Bob.Age);
: : WriteLn(' Your live at :',Bob.Address);
: : End;
: : End.
: :
:
: How will i modify the program because part of my code goes like this..
:
:
: CONST
:
: max= 50;
:
: TYPE
: : Person = record
: : Name : String;
: : Age : Byte;
: : Address : String;
: : End;
: : People = array(1..max) of person;
: : VAR
: friends : people;
:
:
:
:
: my program should be able to save <tt>RECORD of friends</tt> everytime i needed to (this might be before quitting the program or when the user calls the <tt>save function</tt>) and retrieve the saved file (i need to save it on a <tt>*.txt</tt> format) everytime i start the program. You're help Phat Nat is very well appreciated. Thanks.
:
Thank you, I try to help whenever I can.
Is there any particular reason why you need to save it as a .txt file?
Do you need to access it in an outside database because the file will be smaller when it is saved as data (in this case between 100-200 bytes) and the code for doing it is smaller (about 3-4 lines). If you can use the Data format, I would suggest it (as done above) otherwise this would be how you use it as text:
CONST
max= 50;
TYPE
Person = record
Name : String;
Age : Byte;
Address : String;
End;
People = array(1..max) of person;
VAR
friends : people;
PROCEDURE Save(Info : People);
Begin
Assign(T, 'People.Txt'); Rewrite(T);
For X := 1 to max Do
Begin
WriteLn(T, Info[X].Name);
WriteLn(T, Info[X].Age);
WriteLn(T, Info[X].Address);
End;
Close(T);
End;
PROCEDURE Load(Info : People);
Begin
Assign(T, 'People.Txt');
{$I-} Reset(T); {$I+}
If IOResult <> 0 then
Begin
WriteLn('File not found!');
Exit;
End;
For X := 1 to max Do
Begin
ReadLn(Info[X].Name);
ReadLn(Info[X].Age);
ReadLn(Info[X].Address);
If EOF(T) Then Break; { In case the file isn't long enough }
End;
Close(F);
End;
Begin {Used like this:}
Load(People);
Save(People);
End.
This is how it would be done. The only problam with doing it as text (as you may be able to see) is that if you have 100 different variables in your record, it would take a LONG time to write out all the WriteLn(...); lines. Anyways, hope this helps.
Phat Nat