Originale-mail to me for new edition

 

File types

 

A file is an ordered set of elements of the same type. Standard I/O routines use the predefined TextFile or Text type, which represents a file containing characters organized into lines. For more information about file input and output, see Standard routines and I/O.

To declare a file type, use the syntax

type fileTypeName = file of type

where fileTypeName is any valid identifier and type is a fixed-size type. Pointer types whether implicit or explicit are not allowed, so a file cannot contain dynamic arrays, long strings, classes, objects, pointers, variants, other files, or structured types that contain any of these.

For example,

type

  PhoneEntry = record

    FirstName, LastName: string[20];

    PhoneNumber: string[15];

    Listed: Boolean;

  end;

  PhoneList = file of PhoneEntry;

declares a file type for recording names and telephone numbers.

You can also use the file of ... construction directly in a variable declaration. For example,

var List1: file of PhoneEntry;

The word file by itself indicates an untyped file:

var DataFile: file;

For more information, see Untyped files.

Files are not allowed in arrays or records.

 

Topic groups

 

See also

About records

Arrays: Overview

Structured types: Overview

 

 

译文

 

文件类型

 

文件是相同类型元素的有序集合。标准的I/O例程使用预定义的TextFileText类型,它们表示那些包含由字符组织成若干行的文件。有关文件输入和输出的更多信息,见标准例程和I/O

声明文件类型的语法如下

type fileTypeName = file of type

这里的fileTypeName是任意有效的标识符。隐式指针(如长串、动态数组等)和显式指针(如一般的指针类型、结构指针类型等)都不允许作为文件的元素类型,因此,文件中不能含有动态数组、长串、类、对象、指针、变体、其他文件,或者含有这些类型的结构类型。

例如,

type

  PhoneEntry = record

    FirstName, LastName: string[20];

    PhoneNumber: string[15];

    Listed: Boolean;

  end;

  PhoneList = file of PhoneEntry;

这里声明了一个用于记录名称和电话号码的文件类型。

 

也可以在文件变量声明中直接使用file of ... 结构。例如,

var List1: file of PhoneEntry;

 

保留字file本身表示一个无类型文件:

var DataFile: file;

更多信息见无类型文件

在数组和记录中,文件类型是不允许的。

 

Topic groups

 

See also

About records

Arrays: Overview

Structured types: Overview

 

 

编者注

 

对于“在数组和记录中,文件类型是不允许的”这一说法,原文是“Files are not allowed in arrays or records”。编者认为值得商榷。例如,至少编者在Delphi6中已得到验证,下列声明都是合法的,并且可以在语句中正常使用这些数组变量和记录变量:

 

type

  TByteFile = file of Byte;

  TStringFile = file of string[40];

 

  TByteFileArr = array [1..100] of TByteFile;

  TByteFileFlexibleArr = array of TByteFile;

  TStringFileArr = array [1..20] of TStringFile;

 

  TByteFileRec = record

    FileName: string;

    F: TByteFile;

    case Integer of

      0: (SizeInBytes: Cardinal);

      1: (SizeInKB: Cardinal);

  end;

 

  TStringFileRec = record

    Path: string;

    ShortFileName: string[40];

    F: TStringFile;

    Size: Cardinal;

  end;

 

var

  F1: TByteFile;

  F2: TStringFile;

  A1: TByteFileArr;

  A2: TStringFileArr;

  A3: TByteFileFlexibleArr;

  R1: TByteFileRec;

  R2: TStringFileRec;

begin

  SetLength(A3, 1);

  AssignFile(F1, 'c:\test1.bin');

  AssignFile(F2, 'c:\test2.txt');

  AssignFile(A1[1], 'c:\test1.bin');

  AssignFile(A2[7], 'c:\test2.txt');

  AssignFile(A3[0], 'c:\test2.txt');

  AssignFile(R1.F, R1.FileName);

  with R2 do AssignFile(F, ShortFileName);

  ...

end;

 

因此,可以推定,原文的说法是不准确的(怀疑是Object Pascal较早版本中的特征)。