2004. július 19., hétfő
How to load a file into a TMemoryStream and set the size of a string to contain the whole file
Problem/Question/Abstract:
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:
procedure TForm1.ButtonCombineClick(Sender: TObject);
var
i: Integer;
s: array[1..5] of string;
size: Integer;
Stream: TMemoryStream;
begin
for i := Low(FileList) to High(FileList) do
begin
{Load files into strings}
if FileExists(FileList[i]) then
begin
Stream := TMemoryStream.Create;
try
Stream.LoadFromFile(FileList[i]);
SetLength(s[i], Stream.Size);
Stream.Read(s[i][1], Stream.Size)
finally
Stream.Free
end
end
else
s[i] := '';
end;
end;
{ ... }
Feliratkozás:
Megjegyzések küldése (Atom)
Nincsenek megjegyzések:
Megjegyzés küldése