全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1641
推到 Plurk!
推到 Facebook!

兩個檔案的比對

尚未結案
chead
一般會員


發表:10
回覆:12
積分:4
註冊:2004-08-03

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-08-02 14:46:00 IP:220.130.xxx.xxx 未訂閱
檔案可能為文字檔,也可能為binary檔, 若name一樣則要繼續比較檔案size, 若size一樣則要繼續比對內容, 比對內容時,要指出差別的byte所在 重點是要指出差異的byte是我問題的所在, 請問大家有什麼方法可以找出嗎? 麻煩大家了,謝謝
T.J.K
中階會員


發表:3
回覆:35
積分:57
註冊:2005-06-28

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-08-03 09:44:50 IP:211.78.xxx.xxx 未訂閱
剛好有小朋友有類似的問題,供你參考 unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; Memo1: TMemo; procedure Button1Click(Sender: TObject); private aList: TStringList; bList: TStringList; function fFileSize(var FilePath: String) : LongWord; { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} function TForm1.fFileSize(var FilePath: String):LongWord ; //取File Size var FBuf: LongWord; F: file of Byte; begin Result := 0; if not FileExists(FilePath) then begin ShowMessage('找不到檔案【' FilePath '】'); Exit; end; AssignFile(F, FilePath); Try Reset(F); Result := FileSize(F); finally CloseFile(F); end; end; procedure TForm1.Button1Click(Sender: TObject); var aSize, bSize, cSize: LongWord; aList, bList, cList: TStringList; i : integer; aPath, bPath : String; begin cList := TStringList.Create; aPath := 'D:\a.Txt'; bPath := 'D:\b.Txt'; aSize := fFileSize(aPath); bSize := fFileSize(bPath); aList := TStringList.Create; aList.LoadFromFile(aPath); bList := TStringList.Create; bList.LoadFromFile(bPath); if aList.Count = bList.Count then begin cList := aList; Memo1.Lines[0]:= aPath ' = ' bPath ' 檔案大小。'; end else if aList.Count > bList.Count then begin cList := aList; Memo1.Lines[0]:= aPath ' > ' bPath ' 檔案大小。'; end else if aList.Count < bList.Count then begin cList := bList; Memo1.Lines[0]:= aPath ' < ' bPath ' 檔案大小。'; end; Memo1.Lines.Add(''); if cList.Count > -1 then for i := 0 to cList.Count -1 do if (aList.Count >= i 1) and (bList.Count >= i 1) then begin if aList.Strings[i] <> bList.Strings[i] then begin Memo1.Lines.Add('第' IntToStr(i 1) '錯誤資料:'); Memo1.Lines.Add(' ' aPath ':' aList.Strings[i]); Memo1.Lines.Add(' ' bPath ':' bList.Strings[i]); Memo1.Lines.Add(''); end; end else begin if (bList.Count < i) then begin Memo1.Lines.Add('第' IntToStr(i 1) '錯誤資料:'); Memo1.Lines.Add(' ' aPath ':' aList.Strings[i]); Memo1.Lines.Add(' ' bPath ':'); Memo1.Lines.Add(''); end; if (aList.Count < i) then begin Memo1.Lines.Add('第' IntToStr(i 1) '錯誤資料:'); Memo1.Lines.Add(' ' aPath ':'); Memo1.Lines.Add(' ' bPath ':' bList.Strings[i]); Memo1.Lines.Add(''); end; end; end; end.
chead
一般會員


發表:10
回覆:12
積分:4
註冊:2004-08-03

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-08-04 09:03:42 IP:220.130.xxx.xxx 未訂閱
很感謝T.J.K 的回答, 不過這段程式似乎只有對文字檔有效, 如果是圖檔之類的得到的結果會是相同,可是其實是不同的, 所以有其他的方法也可以含括其它檔嗎, 謝謝
chead
一般會員


發表:10
回覆:12
積分:4
註冊:2004-08-03

發送簡訊給我
#4 引用回覆 回覆 發表時間:2005-08-04 10:37:58 IP:220.130.xxx.xxx 未訂閱
我用了TMemoryStream去比對, 這樣可以含跨許多部分, 只是比對內容的地方有點不曉得怎麼比較, 不過只要確定大小不一樣了就不繼續比對了, 所以大小一樣但是內容不一致的情況還沒遇到, 所以目前這樣就夠了, 還是很感謝T.J.K的分享,謝謝
T.J.K
中階會員


發表:3
回覆:35
積分:57
註冊:2005-06-28

發送簡訊給我
#5 引用回覆 回覆 發表時間:2005-08-04 11:41:18 IP:211.78.xxx.xxx 未訂閱
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private FBuf: PByte; FBufSize: LongWord; function ReadFile(var FileName: String): Boolean; { Private declarations } public { Public declarations } end; type FileInfo = packed record aBuf: PByte; aBufSize: LongWord; bBuf: PByte; bBufSize: LongWord; end; var Form1: TForm1; implementation {$R *.dfm} function TForm1.ReadFile(var FileName: String): Boolean; var F: file of Byte; fSize: LongWord; I: LongWord; begin Result := False; if not FileExists(FileName) then Exit; if Assigned(FBuf) then begin FreeMem(FBuf); FBuf := nil; end; AssignFile(F, FileName); try Reset(F); fSize := FileSize(F); FBufSize := fSize; GetMem(FBuf, FBufSize); BlockRead(F, FBuf^, FBufSize, I); if FBufSize <> I then showmessage('無法開始檔案。'); finally CloseFile(F); end; Result := True; end; procedure TForm1.Button1Click(Sender: TObject); var aFN, bFN: String; FNInfo: FileInfo; p: PByte; i : integer; begin aFN := 'D:\a.Doc'; bFN := 'D:\b.Doc'; if ReadFile(aFN) then begin FNInfo.aBuf := FBuf; FNInfo.aBufSize := FBufSize; end else Exit; if ReadFile(bFN) then begin FNInfo.bBuf := FBuf; FNInfo.bBufSize := FBufSize; end else Exit; end; end. 以上供你參考,希望對你有幫助
系統時間:2024-07-01 2:31:26
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!