Author: Jonas Bilinkevicius
I've come across a problem with my file type. I have a record which has an array of
bytes. However, the size of this array varies, or can vary depending how big the
array needs to be. How can I load the record knowing the size of the array?
Answer:
Here's a code fragment that shows how to load a file into a TMemoryStream and then
set the size of a string (an array element in the example below) to contain the
whole file. The variable s[i] then contains all the bytes of the file:
1 procedure TForm1.ButtonCombineClick(Sender: TObject);
2 var3 i: Integer;
4 s: array[1..5] ofstring;
5 size: Integer;
6 Stream: TMemoryStream;
7 begin8 for i := Low(FileList) to High(FileList) do9 begin10 {Load files into strings}11 if FileExists(FileList[i]) then12 begin13 Stream := TMemoryStream.Create;
14 try15 Stream.LoadFromFile(FileList[i]);
16 SetLength(s[i], Stream.Size);
17 Stream.read(s[i][1], Stream.Size)
18 finally19 Stream.Free
20 end21 end22 else23 s[i] := '';
24 end;
25 end;
26 { ... }