5,379
社区成员
发帖
与我相关
我的任务
分享
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;
//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;
//不会卡死,非常快 //只找到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.
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..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;
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一样无反映。。。不知道应该用什么方法来读取这个文件了。。