Registriert seit: 11. Okt 2003
Ort: Elbflorenz
44.184 Beiträge
Delphi 12 Athens
|
Re: schnell Zeilen zählen
13. Dez 2007, 17: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;
$2B or not $2B
|
|
Zitat
|