0

我在网络上有一个包含 95000 多行的大文本文件

现在例如:

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

我正在使用 wininet 我可以将整个文件加载到字符串中,但是在匹配时需要很多时间并且 gui 挂起

反正有没有做得更快?

编辑:

这是我用来从网络获取 urlcontents 的代码

在按钮 1

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;
4

0 回答 0