Sign in Join
Wiki copytemporary

typed files

Deutsch · English · Polski

Typed file

A typed text file is suitable for processing files of any size. A typed text file consists of individual data records. All records in the file have the same structure. This means that each data record is of the same length.

Set the file and record structure

The structure applies to the file type and the record type. The file and data record must have the same structure.

pascal
 type
   TMemployee = Record
   strName : String[20];
   sinContent : Single;
 end;

Create the file

pascal
var
  datFile : File of TMemployee;
  recData : TMemployee;
begin
  AssignFile(datFile, 'example.txt');

  // An existing file is deleted and re-created
  ReWrite(datFile);

  ...
end;

Close the file

pascal
begin
  ...
  Close(datFile);  // closes the file
  ...
end;

Write data record

pascal
var
  datFile : File of TMemployee;
  recData : TMemployee;
begin
  ...

  // fill the record
  recData.strName := 'abcdefghij';
  recData.sinContent := 1700.21;

  // write the record to the file
  Write(datFile, recData); 

  ...
end;

Read data record

pascal
var
  datFile : File of TMemployee;
  recData : TMemployee;
begin
  AssignFile(datFile, 'example.txt');

  Reset(datFile);          // Set the file pointer to the beginning of the file

  Read(datFile, recData);  // Reads a record from the file
  ...
end;

Read file completely

pascal
var
  datFile : File of TMemployee;
  recData : TMemployee;
begin
  AssignFile(datFile, 'example.txt');
  Reset (datFile);

  while not eof(datFile)      // as long as data can still be read
    do begin
      read(datFile, recData); // read record from file
      ...
    end;

  CloseFile (datFile);
end;

Position pointer on a data record

pascal
begin
  AssignFile(datFile, 'example.txt');
  Reset(datFile);
  ...

  Seek(datFile, 0);        // position file pointer at start of first record
  Read(datFile, recData);  // read first record into recData variable

  recData.sinContent := 1602.22; // update record 

  Seek(datFile, 0);              // position file pointer at start of first record
  Write(datFile, recData);       // write first record back

  ...
end;

See also


From the Free Pascal wiki, page "typed files", as it stood on 2020-03-02 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 1, last changed by WikiArchive .

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.