判断一字符串是否在一个文件中存在?
请各位大侠帮助一下,在下非常感谢!
急!急!急!

解决方案 »

  1.   


    procedure TForm1.Button1Click(Sender: TObject);
    var
      wj:File;
      fb:String[20];
      br:integer;
    begin
      assignfile(wj,'文件名');
      reset(wj,1);
      while not eof(wj) do begin
        blockread(wj,fb,sizeof(fb),br);
        if pos('指定字符',fb)>0 then Button1.Caption:='存在';
      end;
      Closefile(wj);
    end;
      

  2.   

    function ExistStr(const FileName: string; const Str: string): Boolean;
    begin
      Result := False;
      if not FileExist(FileName)then
        Exit;
      with TStringList.Create do
      try
        LoadFromFile(FileName);
        if Pos(str, Text) > 0 then
          Result := True;
      finally
        Free;
      end;
    end;
      

  3.   

    这些方法不应是最好的。虽然zzh26(瞌睡)能完成指定的功能,但速度太慢,所以我们应在指定搜索的速度上进行考虑。
      

  4.   

    我的文件比较大,zzh26(瞌睡)提供的速度很慢,有没有速度快一点,谢谢啦!
      

  5.   

    function ExistStr(const FileName: string; const Str: string): Boolean;
    var
      Stream: TStream;
      S: string;
    begin
      Result := False;
      if not FileExists(FileName) then
        Exit;
      
      Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
      try
        SetString(S, nil, Stream.Size);
        Stream.Read(Pointer(S)^, Size);
        if Pos(str, s) > 0 then
          Result := True;
      finally
        Stream.Free;
      end;
    end;
    这样很快了,我测试了一下,6.72兆的文件只要30毫秒左右
      

  6.   

    function SumStr(const FileName: string; const Str: string): Integer;
    var
      Stream: TStream;
      S: string;
      Sum, Start, StrEnd: Integer;
    begin
      Result := 0;
      if not FileExists(FileName) then
        Exit;
      Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
      try
        SetString(S, nil, Stream.Size);
        Stream.Read(Pointer(S)^, Stream.Size);
        Start := 1;
        StrEnd := Pos(Str, s);
        while StrEnd > 0 do
        begin
          Inc(Result);
          Start := StrEnd + Length(Str) + 1;
          s := Copy(S, Start, MaxInt);
          StrEnd := Pos(Str, s);
        end;
      finally
        Stream.Free;
      end;
    end;
    模仿delphi的stringreplace函数写的,如果统计的个数多了还是很慢的,还是上面那个文件,如果出现次数为551次则要7秒钟,如果次数少还是挺快的
      

  7.   

    谢谢各位大侠!wellgsy(50)..该问题已揭贴。