delphitfilestream

Isn't the size of Delphi TFileStream WriteBuffer Int64? I'm only able to write small amounts at a time


I'm trying to write larger chunks to increase the speed of the file save. I have about 9 of these loops to convert but I can't figure out what I'm doing wrong

fs := TFileStream.Create(Myfile, fmCreate);

This code works:

for RecordGroup := 0 to TotalGroups - 1 do
begin
  for RecordNumber := 0 to Length(MyArray[RecordGroup]) - 1 do
  begin
    fs.WriteBuffer(MyArray[RecordGroup,RecordNumber],SizeOf(MyRecord));
  end;
end;

When I remove the innerloop to write bigger chunks, the code does not work:

for RecordGroup := 0 to TotalGroups - 1 do
begin
  fs.WriteBuffer(MyArray[RecordGroup],SizeOf(MyRecord) * Length(MyArray[RecordGroup]));
end;

I get the generic error 'Stream write error'

The value of SizeOf(MyRecord) * Length(MyArray[RecordGroup]) is 180 * 152,004 = 27,360,720

Everything I've read basically says this is correct. Any ideas what I'm doing wrong?

Thanks in advance for any ideas you may have.


Solution

  • Change writing code to (note additional 0 in square brackets)

    fs.WriteBuffer(MyArray[RecordGroup, 0],   SizeOf(MyRecord) * Length(MyArray[RecordGroup]));
    

    Mistake was in wrong dynamic array usage. I assume that MyArray is two-dimensional array, so MyArray[RecordGroup] is 1D dynamic array - essentially pointer to data - but untyped var-parameter of WriteBuffer requires using of array data body.


    Aside note - your for-loop counter goes from 0 to TotalGroups, so overall number of loops is TotalGroups + 1. Is it correct?