Skip Navigation Links.
Record I/O - Text Files.
Language(s):Delphi
Category(s):Record IO

Reading and writing text files using records.

{
  This sample code is presented as is.
  Although every reasonable effort has been
  made to insure the soundness of the example
  below, Idioma Software inc. makes no warranty
  of any kind with regard to this program sample
  either implicitly or explicitly.

  This program example may be freely distributed for the
  use of writing computer programs only. Any other use of
  this material requires written permission from Idioma Software inc.

  (c) 1997 Idioma Software inc. All rights reserved.
 }

{
   This example shows how to do IO using records.
 }
Procedure RecIO;
{Author Jon Vote 10/97}

type {Define a sample record}
     EmpRec = Record
       LastName       : string[25];
       FirstName      : string[25];
       MI             : string[1];
       EmployeeNumber : integer;
     End; {IORec}

const FILE_NAME = 'iotest.tmp';

var IORec    : EmpRec;         {Record Varibale}
    f        : file of EmpRec; {File variable}
    SearchRec: TSearchRec;    {Used with FindFirst}

begin
  {Assign the file varaible}
  assignfile(f, FILE_NAME);

  {create the file if it doesn't exist}
  if FindFirst(FILE_NAME, 0, SearchRec) <> 0 then
      rewrite(f);
  FindClose(SearchRec);  {Always do this after FindFirst, FindNext}

  {Point to beginning of file}
  reset(f);

  {Write a few sample values to the file}
  IORec.LastName := 'Public';
  IORec.FirstName := 'John';
  IORec.MI := 'Q';
  IORec.EmployeeNumber := 100;
  write(f, IORec);

  IORec.LastName := 'Doe';
  IORec.FirstName := 'Jane';
  IORec.MI := 'E';
  IORec.EmployeeNumber := 200;
  write(f, IORec);

  IORec.LastName := 'Homeowner';
  IORec.FirstName := 'Harry';
  IORec.MI := 'P';
  IORec.EmployeeNumber := 300;
  write(f, IORec);

  {Read the values back}
  reset(f);
  While not eof(f) do begin
    read(f, IORec);
    ShowMessage (IORec.LastName + ',' + IORec.FirstName + ' '
      + IORec.MI + ' ' + IntToStr(IORec.EmployeeNumber));
  end; {While}
  {Add another record to the end}
  Seek(f, FileSize(f));
  IORec.LastName := 'Homemaker';
  IORec.FirstName := 'Suzy';
  IORec.MI := 'B';
  IORec.EmployeeNumber := 400;
  write(f, IORec);

  {Change the 2nd entry's LastName field}
  {Note: The first entry is 0}
  reset(f);
  Seek(f, 1);
  read(f, IORec);
  Seek(f, 1);
  IORec.LastName := 'Smith';
  write(f, IORec);

  {Show the change}
  reset(f);
  While not eof(f) do begin
    read(f, IORec);
    ShowMessage (IORec.LastName + ',' + IORec.FirstName + ' '
      + IORec.MI + ' ' + IntToStr(IORec.EmployeeNumber));
  end; {While}


  {Delete the file}
  Erase(f);

  CloseFile(F);

end; { RecIO }

This article has been viewed 4442 times.
The examples on this page are presented "as is". They may be used in code as long as credit is given to the original author. Contents of this page may not be reproduced or published in any other manner what so ever without written permission from Idioma Software Inc.