用流读写结构化文件
unit Unit1;
interface
uses
Windows, Meages, SysUtils, Variants, Claes, Graphics, Controls, Forms,
Dialogs, StdCtrl
type
TForm1 = cla(TForm)
Memo1: TMemo;
{
添加 Memo 显示内容
}
Button1: TButto
Button2: TButto
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{
Private declaratio
}
public
{
Public declaratio
}
end;
var
Form1: TForm1;
implementation
{$R *.dfm
}
type
TRec = record
{
定义一个记录
}
name: string[8];
age: Word;
end;
//写入
procedure TForm1.Button1Click(Sender: TObject);
var
rec: TRec;
ms: TMemoryStream;
begin
ms := TMemoryStream.Create;
rec.name := '张三'; rec.age := 8;
ms.Write(rec, SizeOf(rec));
rec.name := '李四'; rec.age := 81;
ms.Write(rec, SizeOf(rec));
rec.name := '王二麻子'; rec.age := 18;
ms.Write(rec, SizeOf(rec));
ms.SaveToFile('c:/temp/rec.dat');
ms.Free;
end;
//读取
procedure TForm1.Button2Click(Sender: TObject);
var
rec: TRec;
ms: TMemoryStream;
begin
ms := TMemoryStream.Create;
ms.LoadFromFile('c:/temp/rec.dat');
Memo1.Clear;
ms.Position := 0;
while ms.Position < ms.Size do
begin
ms.Read(rec, SizeOf(rec));
Memo1.Lines.Add(rec.name + ' ' + IntToStr(rec.age));
end;
{
Memo1 的显示结果会是:
张三 8
李四 81
王二麻子 18
}
本文来自博客园,作者:{咏南中间件},转载请注明原文链接:https://www.cnblogs.com/hnxxcxg/archive/2010/09/18/2940678.html