I'll try to explain everything...
Program Solution;
Uses Crt;
type data=record
name:string;
tel :string;
end;
var
infor:array[1..2] of data;
F:file of data;
i:Byte;{our little helper...}
Begin {the main program}
ClrScr;
{
here you assign the file's variable to the file.
Remember that the 'file' word is reserved word!
}
Assign(F,'Yourfile.dat');
{
Ok.Now we want to create this file,or if it is exists then
delete its contents!
}
Rewrite(F);
{
now we want to write to this file 2 records..
let's see...
}
i:=1;
WriteLn(i,'.Name: ');
ReadLn(infor[1].name);{ok,we get the first field of the first record}
WriteLn('Her/his phone: ');
ReadLn(infor[1].tel);{and the phone too}
Inc(i);
WriteLn(i,'.Name: ');
ReadLn(infor[2].name);{ and we get the first field of the second record}
WriteLn('Her/his phone: ');
ReadLn(infor[2].tel);{and the phone too}
{
ok,our array is filled by the user
}
WriteLn;{just an empty line...}
{
now we want to write this data to that file,assigned by F
we use the 'for loop' to write the data,so we do not need
to increase the value of 'i'.
}
For i:=1 To 2 Do{'cause our array has two elements(records)}
Begin
Write(F,infor[i]);{do you see???}
End;{of for loop}
{
ok,now our array is written to the file!
now we try to read it back
}
{
but first we close this file..
}
Close(F);
{
opening for reading...
}
Reset(F);
{
we read the data back,and write to the screen...
}
i:=1;
While Not (EoF(F)) Do
Begin
Read(F,infor[i]);
Write(i,'.Name: ',infor[i].name,' Tel: ',infor[i].tel);
WriteLn;
Inc(i);{same as i:=i+1}
End;{of while}
{
and last,we close the file again...
}
Close(F);
ReadLn;
End. {end of main program}
{Created in Turbo Pascal 7.0. by FlamingClaw 2009.08.14.} :D