program matrixtofile;
uses
SysUtils;
type
{ It is assumed that the first index is the row index, the second index is the
column index
( m00 m01 m02 m03 m04 ... )
( m10 m11 m12 m13 m14 ... )
( ... ) }
TMatrix = array of array of Double;
TMatrix_2x3 = packed array[0..1, 0..2] of Double;
procedure SaveMatrixToFile(const M: TMatrix; AFileName: String);
var
F: File;
nr, nc, r: Integer;
begin
if FileExists(AFileName) then
DeleteFile(AFileName);
AssignFile(F, AFileName);
Rewrite(F);
// Write the number of rows first
nr := Length(M);
BlockWrite(F, nr, SizeOf(nr));
// Write the number of columns
nc := Length(M[0]);
BlockWrite(F, nc, SizeOf(nc));
// Write rows - there's number after number in the rows (but not row by row!)
for r := 0 to nr-1 do
// Per row, there are nc columns, each one has the size of a Double
BlockWrite(F, M[r, 0], nc*SizeOf(Double));
CloseFile(F);
end;
procedure SaveMatrix2x3ToFile(const M: TMatrix_2x3; AFileName: String);
var
F: File;
begin
if FileExists(AFileName) then
DeleteFile(AFileName);
AssignFile(F, AFileName);
Rewrite(F);
// Write the matrix in one single call. This is possible because the numbers
// follow one each other. The first number is M[0,0].
BlockWrite(F, M, SizeOf(TMatrix_2x3));
CloseFile(F);
end;
procedure LoadMatrixFromFile(var M: TMatrix; AFileName: String);
var
F: File;
nr: Integer = 0;
nc: Integer = 0;
r: Integer;
n: Integer;
begin
AssignFile(F, AFileName);
Reset(F);
// Read number of rows
BlockRead(F, nr, SizeOf(nr));
// Read number of cols
BlockRead(F, nc, SizeOf(nc));
// Allocate memory for matrix data
SetLength(M, nr, nc);
// Read row by row
for r := 0 to nr-1 do
begin
BlockRead(F, M[r, 0], nc*SizeOf(Double), n);
if n <> nc*SizeOf(Double) then
WriteLn('Reading dynamic matrix: Mismatch in bytes read.');
end;
CloseFile(F);
end;
procedure LoadMatrix2x3FromFile(var M: TMatrix_2x3; AFileName: String);
var
F: File;
begin
AssignFile(F, AFileName);
Reset(F);
BlockRead(F, M[0, 0], SizeOf(TMatrix_2x3));
CloseFile(F);
end;
var
M: TMatrix;
M23: TMatrix_2x3;
i, j: Integer;
begin
// Begin with static array
WriteLn('*** STATIC ARRAY ***');
M23[0, 0] := 0.5;
M23[0, 1] := 2.4;
M23[0, 2] := 1.2;
M23[1, 0] := 3.4;
M23[1, 1] := 5.3;
M23[1, 2] := 6.5;
for i := 0 to 1 do
begin
for j := 0 to 2 do
Write(M23[i, j]:5:1);
WriteLn;
end;
WriteLn;
SaveMatrix2x3ToFile(M23, 'test23.mat');
FillChar(M23, Sizeof(M23), 0);
LoadMatrix2x3FromFile(M23, 'test23.mat');
for i := 0 to 1 do
begin
for j := 0 to 2 do
Write(M23[i, j]:5:1);
WriteLn;
end;
WriteLn;
// Now repeat with dynamic array
WriteLn('*** DYNAMIC ARRAY ***');
SetLength(M, 2, 3);
M[0, 0] := 0.5;
M[0, 1] := 2.4;
M[0, 2] := 1.2;
M[1, 0] := 3.4;
M[1, 1] := 5.3;
M[1, 2] := 6.5;
for i := 0 to 1 do
begin
for j := 0 to 2 do
Write(M[i, j]:5:1);
WriteLn;
end;
WriteLn;
SaveMatrixToFile(M, 'test.mat');
for i := 0 to 1 do
for j := 0 to 2 do
M[i, j] := 0;
LoadMatrixFromFile(M, 'test.mat');
for i := 0 to 1 do
begin
for j := 0 to 2 do
Write(M[i, j]:5:1);
WriteLn;
end;
WriteLn;
WriteLn('Press ENTER to quit...');
Readln;
end.