Reputation: 11
I've been working on assignment where I have to simulate an actual booking system.
The user can create an event which actually just names a text file that they can write to later on.
procedure TForm4.Button1Click(Sender: TObject);
var
f: textfile;
usersFilename: string;
begin
usersFilename := Inputbox('Enter the name of the Event', '', '');
AssignFile(f, usersFilename);
ReWrite(f, usersFilename);
WriteLn(f, usersFilename);
CloseFile(f);
Reset(f);
end;
So now I've created a file with the name of the event which they should be able to write to with this
procedure TForm4.Button2Click(Sender: TObject);
var
Customer: TCustomer;
f: textfile;
usersFilename: string;
begin
usersFilename := Inputbox('Event Name', '', '');
AssignFile(f, usersFilename);
with Customer do
begin
FirstName := 'John';
LastName := 'Smith';
EventDate := 'Grimworth';
SeatNumber := '1';
PhoneNumber := '1';
Adress := '7 Park Drive';
end;
end;
Originally I just had inputboxes that would write to the file after the user wrote the name of the event, however after re reading the assignment outline I realised I HAVE to use records, so now I'm trying to use them but I'm not sure where I've gone wrong. No error actually pops up its just if you open the text file there is nothing in it, empty.
Upvotes: 0
Views: 952
Reputation: 6587
If we look at your second block. There are a couple of problems:
File of TCustomer
instead of a TextFile
. Record files are files of the particular type.Write(f, ....)
Reset(f)
to open the file for reading and writing or ReWrite(f)
to create the file (will also blank it if one already exists).To put it all together, your block of code should look something like this:
procedure TForm4.Button2Click(Sender: TObject);
var
Customer: TCustomer;
f: File of TCustomer;
usersFilename: string;
begin
usersFilename := Inputbox('Event Name', '', '');
AssignFile(f, usersFilename);
// Open if it exists or create the file
if FileExists(usersFilename) then
Reset(f)
else
ReWrite(f);
// Set up our data for writing. This information could
// be retrieved from text boxes, etc.
with Customer do
begin
FirstName := 'John';
LastName := 'Smith';
EventDate := 'Grimworth';
SeatNumber := '1';
PhoneNumber := '1';
Adress := '7 Park Drive';
end;
// Write the data to the file
Write(f, Customer);
CloseFile(f);
end;
One thing this is not taking into account is any existing data. It will start writing from the beginning of the file so you should investigate Seek
for that.
Upvotes: 5