user3710090
user3710090

Reputation: 11

Writing to File With A record

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

Answers (1)

Graymatter
Graymatter

Reputation: 6587

If we look at your second block. There are a couple of problems:

  1. You file is the wrong type. It should be a File of TCustomer instead of a TextFile. Record files are files of the particular type.
  2. You are never writing any thing to the file. To write something to the file you need to use something like Write(f, ....)
  3. You don't actually open the file. You need to use 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).
  4. You should close the file when you are finished with it.

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

Related Questions