Board index » delphi » File Limit size with read-write block?

File Limit size with read-write block?

I have written a function to compare files in two listboxes against each
other.  It works fine on files less than 3MB but bombs on anything over
that.  I have included the function and the call to the function if anyone
can help I would really appreciate it.

See the code below
Thanks in advance
Mike Cox
zor...@acceleration.net

FUNCTION.....

function Compare(S1, S2: String): Boolean;
var
  X: Integer;
  FromF, ToF: file;
  NumRead, NumWritten: Word;
  Buf1: array[1..2048] of Char;
  Buf2: array[1..2048] of Char;
begin
  X := 0;
    AssignFile(FromF, S1);{ Assign Source File}
    Reset(FromF, 1);  { Record size = 1 }
    AssignFile(ToF, S2); { Assign Compare to File }
    Reset(ToF, 1);  { Record size = 1 }
      repeat
        X := X + 1;
        BlockRead(FromF, Buf1, SizeOf(Buf1), NumRead);
        BlockRead(ToF, Buf2, SizeOf(Buf2), NumWritten);
      until (NumRead = 0) or (NumWritten <> NumRead);
        if Buf1[X] <> Buf2[X] then
          begin
            ShowMessage(S1 + 'was not the same as ' + S2);
            Form1.Memo1.Lines.Add(Buf1[X] + ' Located at position: '+
IntToStr(X) + ' '  + ' compared as ' + Buf2[X]);
            Result := False;
        end else
          System.CloseFile(FromF);
          System.CloseFile(ToF);

    end;
--------------------------------------------------------------------->cut
here<----------------------------------
Here is the Call to the Function.....

procedure TForm1.Button6Click(Sender: TObject);
var
  Listc, Listd, Y: Integer;
begin
  if OpenDialog1.Execute then
    begin
      ListBox1.Items := OpenDialog1.Files;{Load the compare from ListBox}
    end;
  if OpenDialog2.Execute then
    begin
      ListBox2.Items := OpenDialog2.Files;{Load the compare to ListBox}
    end;
  Listc := ListBox1.Items.Count;{Get the number of items in the list box}
  Listd := ListBox2.Items.Count;{Get the number of items in the list box}

    {Insert Code here to check that files and positions are the same in the
listboxes}

if ListBox1.Items.Count = ListBox2.Items.Count then
    begin
      for Y := 0 to Listc -1 do {Run the compare function for each file}
        begin
         Compare( ListBox1.Items[Y] , ListBox2.Items[Y]);
        end
    end else
     MessageDlg('Could not complete ListBoxes are not the same.', mtError,
[mbOk], 0);
end;

 

Re:File Limit size with read-write block?


Hi, Mike!

Quote
> I have written a function to compare files in two listboxes against each
> other.  It works fine on files less than 3MB but bombs on anything over
> that.  I have included the function and the call to the function if anyone
> can help I would really appreciate it.

Have a look at your code:

Quote
>   Buf1: array[1..2048] of Char;
>   Buf2: array[1..2048] of Char;
> begin
>   X := 0;
>       repeat
>         X := X + 1;
>         BlockRead(FromF, Buf1, SizeOf(Buf1), NumRead);
>         BlockRead(ToF, Buf2, SizeOf(Buf2), NumWritten);
>       until (NumRead = 0) or (NumWritten <> NumRead);
>         if Buf1[X] <> Buf2[X] then

What you are really doing is reading and _dropping_ the file contents!
Instead of comparing all the file you just compare Byte X in the last
block you have read.
No error will occur if X is less or equal to 2048 for this is the
limit of your array.
So if the file size is more than 2048 times the buffer size (that is
2048 x 2048 bytes) your comparison Buf1[X] <> Buf2[X] will possibly
access memory outside your data segment.

What you have to do is add a loop for comparing the contents of the
two buffers _inside_ the repeat loop!

Klaus

Re:File Limit size with read-write block?


I don't think the function does a too good compare as you keep reading
blocks till the end of one of the files and only when u reach that you
compare the buffers.

That is if I read it correctly as it's still to early to have my brain
awake.

Roel

PS. dunno why it bombs at 3Mb.

Quote
Mike Cox <zor...@acceleration.net> wrote:
> I have written a function to compare files in two listboxes against each
> other.  It works fine on files less than 3MB but bombs on anything over
> that.  I have included the function and the call to the function if anyone
> can help I would really appreciate it.
> See the code below
> Thanks in advance
> Mike Cox
> zor...@acceleration.net
> FUNCTION.....
> function Compare(S1, S2: String): Boolean;
> var
>   X: Integer;
>   FromF, ToF: file;
>   NumRead, NumWritten: Word;
>   Buf1: array[1..2048] of Char;
>   Buf2: array[1..2048] of Char;
> begin
>   X := 0;
>     AssignFile(FromF, S1);{ Assign Source File}
>     Reset(FromF, 1);  { Record size = 1 }
>     AssignFile(ToF, S2); { Assign Compare to File }
>     Reset(ToF, 1);  { Record size = 1 }
>       repeat
>         X := X + 1;
>         BlockRead(FromF, Buf1, SizeOf(Buf1), NumRead);
>         BlockRead(ToF, Buf2, SizeOf(Buf2), NumWritten);
>       until (NumRead = 0) or (NumWritten <> NumRead);
>         if Buf1[X] <> Buf2[X] then
>           begin
>             ShowMessage(S1 + 'was not the same as ' + S2);
>             Form1.Memo1.Lines.Add(Buf1[X] + ' Located at position: '+
> IntToStr(X) + ' '  + ' compared as ' + Buf2[X]);
>             Result := False;
>         end else
>           System.CloseFile(FromF);
>           System.CloseFile(ToF);
>     end;
> --------------------------------------------------------------------->cut
> here<----------------------------------
> Here is the Call to the Function.....
> procedure TForm1.Button6Click(Sender: TObject);
> var
>   Listc, Listd, Y: Integer;
> begin
>   if OpenDialog1.Execute then
>     begin
>       ListBox1.Items := OpenDialog1.Files;{Load the compare from ListBox}
>     end;
>   if OpenDialog2.Execute then
>     begin
>       ListBox2.Items := OpenDialog2.Files;{Load the compare to ListBox}
>     end;
>   Listc := ListBox1.Items.Count;{Get the number of items in the list box}
>   Listd := ListBox2.Items.Count;{Get the number of items in the list box}
>     {Insert Code here to check that files and positions are the same in the
> listboxes}
> if ListBox1.Items.Count = ListBox2.Items.Count then
>     begin
>       for Y := 0 to Listc -1 do {Run the compare function for each file}
>         begin
>          Compare( ListBox1.Items[Y] , ListBox2.Items[Y]);
>         end
>     end else
>      MessageDlg('Could not complete ListBoxes are not the same.', mtError,
> [mbOk], 0);
> end;

Other Threads