Thema: Delphi schnell Zeilen zählen

Einzelnen Beitrag anzeigen

Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
43.139 Beiträge
 
Delphi 12 Athens
 
#9

Re: schnell Zeilen zählen

  Alt 13. Dez 2007, 16:14
iRead, falls die Letzte Zeile nicht den Puffer ausfüllte
Delphi-Quellcode:
iRead := 0;
while not EOF(fInput) do
begin
  BlockRead(fInput, cBuffer, BUFFER_SIZE, iRead);
  sBuffer := Copy(cBuffer, 1, iRead);
  Inc(Result, PosCount(#10, Copy(cBuffer, 1, iRead)));
end;
if (iRead > 0) and (sBuffer[iRead] <> #10) then Inc(Result);
direkt in den String eingelesen (PosCount/PosEx möchten das ja eh in einem String) und intern gezählt:
Delphi-Quellcode:
function CountLines2(const sFile: String): Integer;
const
  BUFFER_SIZE = 65536;
var
  fInput: File;
  S: String;
  iRead, iPos: Integer;
begin
  Result := 0;
  AssignFile(fInput, sFile);
  try
    Reset(fInput, 1);
    try
      iRead := 0;
      while not EOF(fInput) do
      begin
        SetLength(S, BUFFER_SIZE);
        BlockRead(fInput, S[1], BUFFER_SIZE, iRead);
        SetLength(S, iRead);
        iPos := 0;
        repeat
          iPos := PosEx(#0, S, Succ(iPos));
          if iPos > 0 then Inc(Result);
        until iPos = 0;
      end;
      if (iRead > 0) and (S[iRead] <> #10) then Inc(Result);
    finally
      CloseFile(fInput);
    end;
  except
    //Result := -1;
  end;
end;
Garbage Collector ... Delphianer erzeugen keinen Müll, also brauchen sie auch keinen Müllsucher.
my Delphi wish list : BugReports/FeatureRequests
  Mit Zitat antworten Zitat