Code:
unit FMain;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Buttons;
const
REC_COUNT = 4;
type
TRMap = record
X, Y, Index: Integer;
end;
TForm1 = class(TForm)
LoadDat: TBitBtn;
Save: TButton;
ListBox1: TListBox;
LoadMap: TButton;
procedure LoadDatClick(Sender: TObject);
procedure SaveClick(Sender: TObject);
procedure LoadMapClick(Sender: TObject);
private
{ Private declarations }
FS: TFileStream;
ARMap: array of TRMap;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.LoadDatClick(Sender: TObject);
var
i, FileSize: Integer;
begin
try
FS := TFileStream.Create(ExtractFilePath(Paramstr(0)) + 'test.dat', fmOpenRead);
except
ShowMessage('Couldn''t open file.');
Exit;
end;
FS.ReadBuffer(FileSize, SizeOf(FileSize));
// Fs.ReadBuffer(FileSize, FS.Size div SizeOf(Integer));
SetLength(ARMap, FileSize);
FS.ReadBuffer(ARMap[0], SizeOf(TRMap) * FileSize);
Listbox1.Clear;
for i := 0 to FileSize-1 do
begin
{
FS.ReadBuffer(ARMap[i].X, SizeOf(ARMap[i].X));
FS.ReadBuffer(ARMap[i].Y, SizeOf(ARMap[i].Y));
FS.ReadBuffer(ARMap[i].Index, SizeOf(ARMap[i].Index));
}
with Listbox1.Items do
begin
Add(' ');
Add(' *** Record NO: ' + InttoStr(i));
Add(' ');
Add('ARMap['+ InttoStr(i) +'].X: ' + InttoStr(ARMap[i].X));
Add('ARMap['+ InttoStr(i) +'].Y: ' + InttoStr(ARMap[i].Y));
Add('ARMap['+ InttoStr(i) +'].Index: ' + InttoStr(ARMap[i].Index));
end; // with
end; // for
FS.Free;
end; // procedure
procedure TForm1.SaveClick(Sender: TObject);
var
i, liX, liY, liL: Integer;
begin
SetLength(ARMap, REC_COUNT);
FS := TFileStream.Create(ExtractFilePath(Paramstr(0)) + 'test.dat', fmCreate);
// save record number
// FS.WriteBuffer(ARMap, SizeOf(Length(ARMap)));
Listbox1.Clear;
// for i := 0 to REC_COUNT-1 do
for i := 0 to Length(ARMap)-1 do
begin
ARMap[i].X := (i+1);
ARMap[i].Y := (i+1);
ARMap[i].Index := (i+1);
with Listbox1.Items do
begin
Add(' ');
Add(' *** Record NO: ' + InttoStr(i));
Add(' ');
Add('ARMap['+ InttoStr(i) +'].X: ' + InttoStr(ARMap[i].X));
Add('ARMap['+ InttoStr(i) +'].Y: ' + InttoStr(ARMap[i].Y));
Add('ARMap['+ InttoStr(i) +'].Index: ' + InttoStr(ARMap[i].Index));
end; // with
// just one rec: FS.WriteBuffer(ARMap[i], SizeOf(ARMap[i]));
{ just one rec
FS.WriteBuffer(ARMap[i].X, SizeOf(ARMap[i].X));
FS.WriteBuffer(ARMap[i].Y, SizeOf(ARMap[i].Y));
FS.WriteBuffer(ARMap[i].Index, SizeOf(ARMap[i].Index));
} end; // for
// produces rubish which isn't loaded: FS.WriteBuffer(ARMap, SizeOf(ARMap));
// just one rec:
FS.WriteBuffer(ARMap[0], SizeOf(TRMap) * REC_COUNT);
// just one rec: FS.WriteBuffer(Pointer(ARMap)^, Length(ARMap)* SizeOf(Integer));
FS.Free;
ARMap := nil;
end;
procedure TForm1.LoadMapClick(Sender: TObject);
var
i, FileSize: Integer;
begin
try
FS := TFileStream.Create(ExtractFilePath(Paramstr(0)) + 'test.map', fmOpenRead);
// FS := TFileStream.Create(ExtractFilePath(Paramstr(0)) + 'test.dat', fmOpenRead);
except
ShowMessage('Couldn''t open file.');
Exit;
end;
FS.ReadBuffer(FileSize, SizeOf(FileSize));
// Fs.ReadBuffer(FileSize, FS.Size div SizeOf(Integer));
SetLength(ARMap, FileSize);
FS.ReadBuffer(ARMap[0], SizeOf(TRMap) * FileSize);
Listbox1.Clear;
for i := 0 to FileSize-1 do
begin
{ is working as well, but more mess
FS.ReadBuffer(ARMap[i].X, SizeOf(ARMap[i].X));
FS.ReadBuffer(ARMap[i].Y, SizeOf(ARMap[i].Y));
FS.ReadBuffer(ARMap[i].Index, SizeOf(ARMap[i].Index));
}
with Listbox1.Items do
begin
Add(' ');
Add(' *** Record NO: ' + InttoStr(i));
Add(' ');
Add('ARMap['+ InttoStr(i) +'].X: ' + InttoStr(ARMap[i].X));
Add('ARMap['+ InttoStr(i) +'].Y: ' + InttoStr(ARMap[i].Y));
Add('ARMap['+ InttoStr(i) +'].Index: ' + InttoStr(ARMap[i].Index));
end; // with
end; // for
FS.Free;
end;
end.
Bookmarks