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 2 procedure TForm1.ButtonCombineClick(Sender: TObject);
3 var4 i: Integer;
5 s: array[1..5] ofstring;
6 size: Integer;
7 Stream: TMemoryStream;
8 begin9 for i := Low(FileList) to High(FileList) do10 begin11 {Load files into strings}12 if FileExists(FileList[i]) then13 begin14 Stream := TMemoryStream.Create;
15 try16 Stream.LoadFromFile(FileList[i]);
17 SetLength(s[i], Stream.Size);
18 Stream.read(s[i][1], Stream.Size)
19 finally20 Stream.Free
21 end22 end23 else24 s[i] := '';
25 end;
26 end;
27 { ... }