Delphi基础Write写入结构体到文件(使用 file of myrecord就行了,真简单)

时间:2023-03-09 15:57:15
Delphi基础Write写入结构体到文件(使用 file of myrecord就行了,真简单)
program WriteStruct;

{$APPTYPE CONSOLE}

uses
SysUtils; //写入结构体 type
TCustomer = record
ID: string[];
Code: string[];
Name: string[];
end; var
Customers: array[..] 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 := to 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 := to do begin
Customers[i].ID := '';
Customers[i].Code := '';
Customers[i].Name := '';
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.

http://www.cnblogs.com/pengshaomin/archive/2012/09/28/2707413.html