Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Compare Two Files

sudendrasudendra Member Posts: 17
Is there any ready made function in Delphi by which I can compare two files directly.
What I want is to see if two files are identical or not ?

Comments

  • zibadianzibadian Member Posts: 6,349
    : Is there any ready made function in Delphi by which I can compare two files directly.
    : What I want is to see if two files are identical or not ?
    :
    As far as I know there isn't one, but it is very easy to write it yourself. Here is a very slow, untested function for that:
    [code]
    function FileCompare(Filename1, Filename2: string): boolean;
    var
    f1, f2: file;
    ch1, ch2: char;
    begin
    AssignFile(f1, Filename1);
    Reset(f1, 1);
    AssignFile(f2, Filename2);
    Reset(f2, 1);
    Result := FileSize(f1) = FileSize(f2); { check their sizes }
    while Result and (not eof(f1)) do begin // while size & contents are
    BlockRead(f1, ch1, SizeOf(ch1)); // the same keep on reading
    BlockRead(f2, ch2, SizeOf(ch2));
    Result := ch1 = ch2; // compare a single byte
    end;
    CloseFile(f1);
    CloseFile(f2);
    end;
    [/code]
    This code can be optimalized by creating a buffer to store parts of the file and comparing those buffers.
Sign In or Register to comment.