assfile(f,'a.txt'); reset(f); while not eof(f) do begin Readln(F,S); if Pos('yourfindstring',S)> 0 then begin showmessage('找到了'); break; end; end; closefile(f);
这句:while not eof(f) do 是什么意思啊??
var F: TextFile; S: string; begin if OpenDialog1.Execute then //如果选择文件名成功 begin AssignFile(F, OpenDialog1.FileName); Reset(F); While not Eof(F) do //文件未到末尾 begin Readln(F, S); //读一行 if Pos('yourfindstring',S)> 0 then begin ShowMessage('找到了'); break; end; end; CloseFile(F); end; end; ------------------------------- 我的更详细版本
装载到StringList中更简单. var str :Tstrings; i:integer; begin str :=TStringList.create; str.LoadFromFile('文件全路径'); for i:=0 to str.count-1 do begin if pos('yourstring',str.string[i])>0 then ShowMessage('找到了'); end;
用Pos就可以了!
reset(f);
while not eof(f) do
begin
Readln(F,S);
if Pos('yourfindstring',S)> 0 then
begin
showmessage('找到了');
break;
end;
end;
closefile(f);
F: TextFile;
S: string;
begin
if OpenDialog1.Execute then //如果选择文件名成功
begin
AssignFile(F, OpenDialog1.FileName);
Reset(F);
While not Eof(F) do //文件未到末尾
begin
Readln(F, S); //读一行
if Pos('yourfindstring',S)> 0 then
begin
ShowMessage('找到了');
break;
end;
end;
CloseFile(F);
end;
end;
-------------------------------
我的更详细版本
用memo来操作也可以
memo.lines.LoadFromFile('文件名')
再用pos查找
var
str :Tstrings;
i:integer;
begin
str :=TStringList.create;
str.LoadFromFile('文件全路径');
for i:=0 to str.count-1 do
begin
if pos('yourstring',str.string[i])>0 then
ShowMessage('找到了');
end;