Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Neuen Beitrag zur Code-Library hinzufügen (https://www.delphipraxis.net/33-neuen-beitrag-zur-code-library-hinzufuegen/)
-   -   Delphi Zeilen einer Textdatei zählen (https://www.delphipraxis.net/135303-zeilen-einer-textdatei-zaehlen.html)

TBx 8. Jun 2009 20:25


Zeilen einer Textdatei zählen
 
Christian Seehase stellt hier eine Möglichkeit vor, schnell die Zeilen einer Textdatei zu zählen:

Delphi-Quellcode:
function csCountLines(const AsFilepath : string) : Integer;

const
  BUFSIZE = 65536;

var
  fs    : TFileStream;
  sBuf  : array [1..BUFSIZE] of char;
  iLines : Integer;
  iRead : integer;
  i     : Integer;

begin
  fs := TFileStream.Create(AsFilepath,fmOpenRead);
  try
    iLines := 0;
    iRead := fs.Read(sBuf,BUFSIZE);
    while iRead = BUFSIZE do begin
      for i := 1 to iRead do begin
        Inc(iLines,Ord(sBuf[i]=#10));
      end;
      iRead := fs.Read(sBuf,BUFSIZE);
    end;
    for i := 1 to iRead do begin
      Inc(iLines,Ord(sBuf[i]=#10));
    end;
    if (iRead > 0) and (sBuf[iRead] <> #10) then inc(iLines);
  finally
    fs.Free;
  end;
  Result := iLines;
end;

himitsu 8. Jun 2009 22:25

Re: Zeilen einer Textdatei zählen
 
etwas gekürzt:
Delphi-Quellcode:
function csCountLines(const Filepath : String) : Integer;

const
  BUFSIZE = 65536;

var
  fs   : TFileStream;
  sBuf : array [1..BUFSIZE] of AnsiChar;
  iRead : Integer;
  i    : Integer;

begin
  Result := 0;
  fs := TFileStream.Create(Filepath, fmOpenRead);
  try
    if fs.Size > 0 then Inc(Result);
    repeat
      iRead := fs.Read(sBuf, BUFSIZE);
      for i := 1 to iRead do
        if sBuf[i] = #10 then Inc(Result);
    until iRead <> BUFSIZE;
  finally
    fs.Free;
  end;
end;
Sollte alle #13-#10-Kombinationen erkennen > #10, #13 und #13#10
Delphi-Quellcode:
function csCountLines(const Filepath : String) : Integer;

const
  BUFSIZE = 65536;

var
  fs     : TFileStream;
  sBuf   : array [1..BUFSIZE + 1] of AnsiChar;
  iRead  : Integer;
  iOffset : Integer;
  i      : Integer;

begin
  Result := 0;
  iOffset := 0;
  fs := TFileStream.Create(Filepath, fmOpenRead);
  try
    if fs.Size > 0 then Inc(Result);
    repeat
      iRead := fs.Read(sBuf[iOffset + 1], BUFSIZE) + iOffset;
      iOffset := 0;
      i := 1;
      while i <= iRead do begin
        case sBuf[i] of
          #10: Inc(Result);
          #13: if i < iRead then begin
                 Inc(Result);
                 if sBuf[i + 1] = #10 then Inc(i);
               end else if iRead < BUFSIZE then
                 Inc(Result)
               else begin
                 sBuf[1] := sBuf[iRead];
                 iOffset := 1;
               end;
        end;
        Inc(i);
      end;        
    until iRead <> BUFSIZE;
  finally
    fs.Free;
  end;
end;

und noch eine spezielle Version für Unicode-Dateien:
(die vorherigen Versionen sind für Ansi-Dateien)
Delphi-Quellcode:
function csCountLines(const Filepath : String) : Integer;

const
  BUFSIZE = 65536;

var
  fs     : TFileStream;
  sBuf   : array [1..BUFSIZE + 1] of WideChar;
  iRead  : Integer;
  iOffset : Integer;
  i      : Integer;

begin
  Result := 0;
  iOffset := 0;
  fs := TFileStream.Create(Filepath, fmOpenRead);
  try
    if fs.Size > 0 then Inc(Result);
    repeat
      iRead := fs.Read(sBuf[iOffset + 1], BUFSIZE) + iOffset;
      iOffset := 0;
      i := 1;
      while i <= iRead do begin
        case sBuf[i] of
          #$0A, #$0085, #$2028: Inc(Result);
          #$0D: if i < iRead then begin
                  Inc(Result);
                  if (sBuf[i + 1] = #$0A) or (sBuf[i + 1] = #$0085) then Inc(i);
                end else if iRead < BUFSIZE then
                  Inc(Result)
                else begin
                  sBuf[1] := sBuf[iRead];
                  iOffset := 1;
                end;
        end;
        Inc(i);
      end;        
    until iRead <> BUFSIZE;
  finally
    fs.Free;
  end;
end;


Alle Zeitangaben in WEZ +1. Es ist jetzt 00:27 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz