Search code examples
delphisplitcopymemory

Delphi Split a string with CopyMemory in part-strings using an offset


I want to split a string in X partstrings.

This is my code so far:

procedure SplitFile(var a: StringArray; len: integer; fileString: AnsiString; numberParts: integer);
var
  i, Rest, partSize: integer;
  tmp: AnsiString;
begin
  Rest := len mod numberParts;
  SetLength(a, numberParts);
  partSize := (len - Rest) div numberParts;
  numberParts := numberParts - 1;
  for i := 0 to numberParts do
  begin
    // Get access violation here
    CopyMemory(@a[i][1], @filestring[i * partSize], partSize);
  end;
  if Rest <> 0 then
    CopyMemory(@a[numberParts][numberParts*partSize], @filestring[numberParts*partSize], Rest);
end;

I get an access violation on this line:

CopyMemory(@a[i][1], @filestring[i * partSize], partSize);

I am not sure where exactly the error gets caused. How do I get the Address of a[i], sin't it @a[i][1]? and causes it an error anyway when it tries to access @filestring[i* partSize] in the first loop, cause it would be 0?

I hope someone can explain me that.

Thanks


Solution

  • Your code is over-complicated for what you are attempting. Try this simpler code instead:

    // assuming StringArray is "array of (Ansi)String"...
    procedure SplitFile(var a: StringArray; len: integer; fileString: AnsiString; numberParts: integer);
    var
      i, partSize: integer;
    begin
      partSize := len div numberParts;
      SetLength(a, numberParts);
      for i := 0 to numberParts-1 do begin
        a[i] := Copy(filestring, (i * partSize)+1, partSize);
      end;
    end;
    

    On the last iteration, Copy() will truncate the returned substring for you if the requested count is more than the available number of characters at the requested offset.