program WriteStruct;
{$APPTYPE CONSOLE}
uses
SysUtils;
//写入结构体
type
TCustomer = record
ID: string[10];
Code: string[10];
Name: string[10];
end;
var
Customers: array[1..10] of TCustomer;
i: Integer;
myText: file of TCustomer; //file of type type必须是固定大小的,不能是对象, String, Variant等
customer: TCustomer;
begin
try
AssignFile(myText, 'D:\customer.cus');
//重写文件
Rewrite(myText);
for i := 1 to 10 do begin
Customers[i].ID := 'Test:' + IntToStr(i);
Customers[i].Code := 'Buy:' + IntToStr(i);
Customers[i].Name := 'monty' + IntToStr(i);
Write(myText, Customers[i]);
end;
CloseFile(myText);
for i := 1 to 10 do begin
Customers[i].ID := '0';
Customers[i].Code := '0';
Customers[i].Name := '0';
end;
//只读模式打开文件
Reset(myText);
//读取文件是否结束
while not Eof(myText) do begin
read(myText, customer);
//输出
Writeln(Customer.ID, customer.Name, customer.Code);
end;
CloseFile(myText);
Readln;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.