How to load a file into a TMemoryStream and set the size of a string to contain the whole file (Views: 100)


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?


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:

procedure TForm1.ButtonCombineClick(Sender: TObject);
  i: Integer;
  s: array[1..5] of string;
  size: Integer;
  Stream: TMemoryStream;
  for i := Low(FileList) to High(FileList) do
    {Load files into strings}
    if FileExists(FileList[i]) then
      Stream := TMemoryStream.Create;
        SetLength(s[i], Stream.Size);
        Stream.Read(s[i][1], Stream.Size)
      s[i] := '';
{ ... }

<< Back to main page