AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Zeilen einer Textdatei zählen

Ein Thema von TBx · begonnen am 8. Jun 2009 · letzter Beitrag vom 8. Jun 2009
Antwort Antwort
TBx
(Administrator)

Registriert seit: 13. Jul 2005
Ort: Stadthagen
1.875 Beiträge
 
Delphi 12 Athens
 
#1

Zeilen einer Textdatei zählen

  Alt 8. Jun 2009, 20:25
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;
Thomas Breitkreuz
  Mit Zitat antworten Zitat
Benutzerbild von himitsu
himitsu

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

Re: Zeilen einer Textdatei zählen

  Alt 8. Jun 2009, 22:25
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;
Garbage Collector ... Delphianer erzeugen keinen Müll, also brauchen sie auch keinen Müllsucher.
my Delphi wish list : BugReports/FeatureRequests
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 06:07 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