想要实现的功能:查找指定内容,如果找到,则输出当前行,以及当前行以下的六行内容,
procedure TForm1.Button1Click(Sender: TObject);
var
Ftext: TextFile;
fn,lninfo:string;
res,res1:string;
intext,outtext:string;
lnnum:integer;
findln:integer;
lncount:integer;
begin
intext:=edit1.Text ;
outtext:='';
fn:= 'c:\text.txt';
if FileExists(fn) then
begin
AssignFile(Ftext,fn);
lnnum:=0;
findln:=0;
while (not eof(Ftext)) and (findln=0) do
begin
Readln(Ftext,lninfo);
lnnum:=lnnum+1;
findln:=0;
if (pos(intext,lninfo) >0) then
begin
findln:=1;
outtext:=lninfo;
end;
end;
lncount:=0;
while (not eof(Ftext)) and (lncount<=6) and (findln=1) do
begin
Readln(Ftext,lninfo);
outtext:=outtext+lninfo;
lncount:=lncount+1;
end;
CloseFile(Ftext);
memo1.text:= outtext ;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Ftext: TextFile;
fn,lninfo:string;
res,res1:string;
intext,outtext:string;
lnnum:integer;
findln:integer;
lncount:integer;
begin
intext:=edit1.Text ;
outtext:='';
fn:= 'c:\text.txt';
if FileExists(fn) then
begin
AssignFile(Ftext,fn);
lnnum:=0;
findln:=0;
while (not eof(Ftext)) and (findln=0) do
begin
Readln(Ftext,lninfo);
lnnum:=lnnum+1;
findln:=0;
if (pos(intext,lninfo) >0) then
begin
findln:=1;
outtext:=lninfo;
end;
end;
lncount:=0;
while (not eof(Ftext)) and (lncount<=6) and (findln=1) do
begin
Readln(Ftext,lninfo);
outtext:=outtext+lninfo;
lncount:=lncount+1;
end;
CloseFile(Ftext);
memo1.text:= outtext ;
end;
end;
还差一句 reset(Ftext);
如果你要读六行,这个要改一下
while (not eof(Ftext)) and (lncount <=5) and (findln=1) do