procedure TForm1.Button1Click(Sender: TObject);
var f: TextFile; s: string; sPos, ePos: Integer; sL: TStringList;
begin
sL := TStringList.Create;
AssignFile(f, 'a.log');
reset(f);
while not eof(f) do begin
Readln(f, s); // 賦值給S。
if Pos('TXN=', s) > 0 then begin // 找到指定字串所在行
sPos := Pos('=', s); // 查找"="的位置
ePos := Pos(';', s); // 查找";"的位置
sL.Add(Copy(s, sPos + 1, ePos - sPos - 1)); // 含有";"就獲取":"到";"的字串
s1 := (sL.Text);
sL.Clear;
showmessage(s1);
end;
if Pos('TXP=', s) > 0 then begin // 找到指定字串所在行
sPos := Pos('=', s); // 查找"="的位置
ePos := Pos(';', s); // 查找";"的位置
sL.Add(Copy(s, sPos + 1, ePos - sPos - 1)); // 含有";"就獲取":"到";"的字串
s2 := (sL.Text);
sL.Clear;
showmessage(s2);
end;
if Pos('"success":true,', s) > 0 then begin
showmessage('找到了');
end;
end;
FreeAndNil(sL);
closefile(f);
end;
用該按鍵事件對無亂碼的檔案查找是正常的,如果檔案中包含有亂碼就不正常了,求大神指點,附上帶亂碼檔案。。
http://182.129.243.99:8000/aaa/a.log
uj5u.com熱心網友回復:
procedure TForm1.Button2Click(Sender: TObject);
var
SL : TStringList;
i:string;
begin
SL := TStringList.Create;
SL.LoadFromFile('a.log',TEncoding.UTF8);
if SL.IndexOf('TXN=') > 0 then
begin
showmessage('ok');
end;
SL.Free;
end;
用這個辦法也試過,更改編碼UTF8,運行會報錯,ANSI一樣無反映。。。不知道應該用什么方法來讀取這個檔案了。。
uj5u.com熱心網友回復:
procedure TForm1.Button2Click(Sender: TObject);
var
SL : TStringList;
i:Integer;
begin
SL := TStringList.Create;
SL.LoadFromFile('a.log');
for i := 0 to SL.Count - 1 do
begin
if Pos('TXN=', SL[i]) <> 0 then
showmessage('ok');
end;
SL.Free;
end;
uj5u.com熱心網友回復:
樓上的大哥,試用了一下可以,但是我用于真正的LOG檔案時就不行了,程式會卡死!!!?? 出現無回應。比如說我現在的LOG檔案是1.5M的大小。上面我的a.log我已重新更新成原本的log檔案了,我在你的基礎上進行了修改你看看有沒有不正確的地方。現在運行是不正常的。
procedure TForm1.Button1Click(Sender: TObject);
var
sL: TStringList;
sPos, ePos,i: Integer;
begin
sL := TStringList.Create;
sL.LoadFromFile('a.log');
for i := 0 to sL.Count - 1 do begin
if Pos('TXN=', sL[i]) > 0 then begin // 找到指定字串所在行
showmessage(sL[i]);
sPos := Pos('=', sL[i]); // 查找"="的位置
ePos := Pos(';', sL[i]); // 查找";"的位置
sL.Add(Copy(sL[i], sPos + 1, ePos - sPos - 1)); // 含有";"就獲取":"到";"的字串
s1 := sL.Text;
sL.Clear;
showmessage(s1);
end;
end;
sL.Free;
end;
主要是為了找出TXN=后面的字串并提取出來賦值給全域變數 S1..
uj5u.com熱心網友回復:
提醒樓主,既然存在所謂的亂碼,那么就有可能存在0x00,而在DELPHI里,按照字串的方式處理,遇到這個會當成字串的結尾,導致后面的內容不處理。所以,我建議樓主用二進制的方式查找替換。也很簡單,將內容讀取到一個流里,然后用CompareMem進行比較。uj5u.com熱心網友回復:
procedure TForm1.Button2Click(Sender: TObject);var
SL : TStringList;
i:string;
begin
SL := TStringList.Create;
SL.LoadFromFile('a.log',TEncoding.UTF8);
if SL.IndexOf('TXN=') > 0 then
begin
showmessage('ok');
end;
SL.Free;
end;
uj5u.com熱心網友回復:
你的檔案呢?傳上來啊uj5u.com熱心網友回復:
//不會卡死,非常快 //只找到TXN的值,其他類推
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
Vcl.StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
function FindPos(const StrBuf, FileBuf: TBytes; BeginNo: Integer=0): Integer;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function TForm1.FindPos(const StrBuf, FileBuf: TBytes; BeginNo: Integer): Integer;
var
I: Integer;
begin
Result := -1;
for I := BeginNo to Length(FileBuf)-Length(StrBuf) do
begin
if CompareMem(@StrBuf[0], @FileBuf[I], Length(StrBuf)) then
begin
Result := I;
Break;
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Stream: TFileStream;
Encoding: TEncoding;
I, Pos1, Pos2, DataLen: Integer;
StrBuf, FileBuf, RstBuf: TBytes;
begin
Encoding := TEncoding.Ansi; //如果檔案編碼為Unicode,則改為TEncoding.Unicode
Stream := TFileStream.Create('a.log', fmOpenRead);
SetLength(FileBuf, Stream.Size);
Stream.ReadBuffer(FileBuf, Stream.Size);
Stream.Free;
StrBuf := Encoding.GetBytes('TXN');
Pos1 := FindPos(StrBuf, FileBuf);
if Pos1 >= 0 then
begin
Pos1 := Pos1 + Length(StrBuf);
StrBuf := Encoding.GetBytes('=');
Pos1 := FindPos(StrBuf, FileBuf, Pos1);
if Pos1 >= 0 then
begin
Pos2 := Pos1 + Length(StrBuf);;
StrBuf := Encoding.GetBytes(';');
Pos2 := FindPos(StrBuf, FileBuf, Pos2);
if Pos2 >= 0 then
begin
if Encoding = TEncoding.Ansi then
begin //Ansi
DataLen := Pos2-Pos1-1;
Pos1 := Pos1 + 1;
end
else
begin //Unicode
DataLen := Pos2-Pos1-2;
Pos1 := Pos1 + 2;
end;
SetLength(RstBuf, DataLen);
for I := 0 to DataLen-1 do
RstBuf[I] := FileBuf[Pos1+I];
ShowMessage('Found: ' + Trim(Encoding.GetString(RstBuf)));
end;
end;
end;
end;
end.
uj5u.com熱心網友回復:
對于來源不明未知格式的檔案可以試試用uedit32打開,然后再找規律。找到規律就簡單了。資料格式的檔案只能用二進制來操作。
procedure readfile(filename:string);//資料檔案,每條記錄長235位元組,包括帳戶(uint),密碼等資訊。
const
ds:integer=235;//每條記錄長235位元組。
var
f:file;
numread,i:integer;
buf:array[0..234] of byte;
acc:uint;
accstr,mm,tmp:string;
begin
assignfile(f,filename);
reset(f,1);
blockread(f,buf,2,numread);//去掉檔案頭標識
blockread(f,buf,ds,numread);
while numread=235 do
begin
tmp:='';
mm:='';
for i:=0 to 7 do
begin
tmp:=tmp+inttohex(ord(buf[i]),2);
end;
acc:=strtoint64('$'+tmp);
accstr:=inttostr(acc);
for i:=8 to 27 do
begin
if buf[i]=0 then break;
mm:=mm+char(buf[i]);
end;
form1.memo1.Lines.Add(accstr+'----'+mm);
blockread(f,buf,ds,numread);
end;
closefile(f);
end;
uj5u.com熱心網友回復:
//Memo1:TMemo;
procedure TForm1.Button1Click(Sender: TObject);
var
MS:TMemoryStream;
p:PChar;
s:string;
procedure Find(ATxt:string);
var
iB,iE:Integer;
begin
iB:=Pos(ATxt,p);
while iB>0 do
begin
inc(p,iB+length(ATxt));
iE:=Pos(';',p);
Memo1.Lines.Add(ATxt);
Memo1.Lines.Add(Copy(p,1,iE-1));
iB:=Pos(ATxt,p);
end;
end;
begin
MS:=TMemoryStream.Create;
try
MS.LoadFromFile('a.log');
p:=PChar(MS.Memory);
Find('TXN=');
p:=PChar(MS.Memory);
Find('TXP=');
p:=PChar(MS.Memory);
s:='"success":true';
if Pos(s,p)>0 then
Memo1.Lines.Add('找到 '+s)
else Memo1.Lines.Add('沒找到 '+s);
finally
MS.Free;
end;
end;
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/36009.html
標籤:VCL組件開發及應用
上一篇:freshman日記
