delphi从web获取并比较web中的大文本文件

时间:2013-10-16 13:32:17

标签: delphi

我有一个大文本文件,网上有95000多行

现在举例:

  • 我有一个字符串
  • 我想在该文件中匹配该字符串 并读取该字符串放在文件中的整行

我正在使用wininet 我可以将整个文件加载到字符串中 虽然匹配它需要很多时间和gui挂起

无论如何更快地做到了吗?

修改

以下是我用来从网站上获取网址的代码

button1

中的

var
s:string;

begin
  //1-  as file is big it takes alot of time :/ 
  S := GetUrlContent('web url example');
  if (Pos('string i want to search', s)> 0) then
  begin 
  mmo1.lines.add('Found');
  //2-  now how can i show extact line that contains string 
  end;
end;

// --------------------------------------------- -------

function GetUrlContent(const Url: string): string;
var
  NetHandle: HINTERNET;
  UrlHandle: HINTERNET;
  Buffer: array[0..1024] of Char;
  BytesRead: dWord;
begin
  Result := '';
  NetHandle := InternetOpen('Delphi 5.x', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);

  if Assigned(NetHandle) then
  begin
    UrlHandle := InternetOpenUrl(NetHandle, PChar(Url), nil, 0, INTERNET_FLAG_RELOAD, 0);

    if Assigned(UrlHandle) then
      { UrlHandle valid? Proceed with download }
    begin
      FillChar(Buffer, SizeOf(Buffer), 0);
      repeat
        Result := Result + Buffer;
        FillChar(Buffer, SizeOf(Buffer), 0);
        InternetReadFile(UrlHandle, @Buffer, SizeOf(Buffer), BytesRead);
      until BytesRead = 0;
      InternetCloseHandle(UrlHandle);
    end
    else
      { UrlHandle is not valid. Raise an exception. }
      raise Exception.CreateFmt('Cannot open URL %s', [Url]);

    InternetCloseHandle(NetHandle);
  end
  else
    { NetHandle is not valid. Raise an exception }
    raise Exception.Create('Unable to initialize');
end;

0 个答案:

没有答案