假设有一个文件 c:\test.txt同时有一数组:  a:array[0..100] of string;要求在文件中查出 a[i] 第一次出现的所在行数
var f:textfile;
    i,j:integer;
    s:string;
begin
    for i:=low(a) to high(a) do begin
       
       j:=0;
       assignfile(f,'c:\test.txt');
       reset(f);
       while not eof(f) do begin
         readln(f,s);
         j:=j+1; 
         if s=a[i] then begin
             memo1.lines.add(inttostr(j));
             continue;     
         end; 
       end;
       closefile(f);
    end;运行后出现死循环请高手指点,谢谢

解决方案 »

  1.   

    assignfile(f,'c:\test.txt');
    效率低啊
      

  2.   

    assignfile(f,'c:\test.txt');
    放在循环中,效率低啊事先加载到一个stringlist中
      

  3.   

    用TStringList
    procedure TForm2.Button1Click(Sender: TObject);
    var
      slFile: TStringList;
      i,j:integer;
      s:string;
    begin
      slFile := TStringList.Create;
      slFile.LoadFromFile('c:\test.txt');
      for i:=low(a) to high(a) do
        for j:=0 to slFile.Count - 1 do
          if slFile.Strings[j]=a[i] then
            memo1.lines.add(inttostr(j+1));
      slFile.Free;
    end;
      

  4.   

    LS有答案了,不过还是要加上Continue,不做无用功
    begin
      memo1.lines.add(inttostr(j+1));
      Continue;
    end;