Name

File Keyword

Syntax

var
  BinaryFile: file;
  TypedFile: file of Some type;

Description

A file is a binary sequence of some type. A file’s contents are usually stored in a persistent medium, such as a disk drive. If you do not specify a type, the untyped file is treated as a sequence of fixed-size records where the record size is determined when you open the file with Reset or Rewrite.

Internally, Delphi represents a binary file as a record. The SysUtils unit declares the TFileRec type for your convenience, or you can declare the record yourself:

type
  TFileRec = packed record
    Handle: Integer;                 // Windows file handle
    Mode: Integer;              // fmInput, fmOutput, fmInOut, fmClosed
    RecSize: Cardinal;               // record size
    Private: array[1..28] of Byte;   // reserved by Delphi
    UserData: array[1..32] of Byte;  // you can use this
    Name: array[0..259] of Char;     // file path
  end;

Tips and Tricks

  • The file type is for binary files. See the TextFile type for text files. Binary and text files often follow completely different rules in Delphi. Note also that the TextFile type is not the same as file of Char.

  • The base type for a file can be a scalar type, an array type, or a record type. It should not be a pointer, dynamic array, or long string. Storing a pointer in a file serves no useful purpose because pointer values are ephemeral. Long and wide strings are pointers, so if you need to store strings in a file, use short strings or call BlockRead and BlockWrite.

  • Call Reset to open an existing file or Rewrite to create a new file.

  • Delphi does not buffer its file I/O, so reading and writing individual records can be slow. Call BlockRead and BlockWrite to handle many records at once to improve performance.

  • Do not use Integer, Cardinal, or any other type whose size can vary from one release to another. Instead, choose a specific size, such as Word or LongInt. That way, files that you create with one version can be read with another version.

  • Alignment of fields within a record can change from one version of Delphi to another. Use packed records to minimize the chances that a record format will change from the version that creates a file to a version that reads a file.

  • Delphi does not support the standard Pascal approach to file I/O: calling Get or Put, and dereferencing a file variable as a pointer. Instead, see the Read, Write, BlockRead, and BlockWrite procedures.

Example

type
  TStudent = packed record
    ID: string[9];
    Name: string[40];
    GPA: Single;
  end;
  TStudentFile = file of TStudent;
var
  F: TStudentFile;
  S: TStudent;
begin
  AssignFile(F, 'students.dat'),
  Reset(F);
  try
    while not Eof(F) do
    begin
      Read(F, S);
      ProcessStudent(S);
    end;
  finally
    CloseFile(F);
  end;
end;

See Also

AssignFile Procedure, BlockRead Procedure, BlockWrite Procedure, Eof Function, FilePos Function, FileSize Function, IOResult Function, Packed Keyword, Read Procedure, Record Keyword, Seek Procedure, TextFile Type, Write Procedure
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset