0 Members and 2 Guests are viewing this topic.
function GetLines(FileDir: string): integer;var i,lines: integer; file: string;begin Lines := 0; file :=ReadFile(Filedir); For i := 1 to 1000 do begin If GetPiece(file,chr(13) + chr(10),i) <> '' then begin lines := i+1; end else break; end; Result := lines;end;
function DoesFileExist(Name: string): boolean;begin if (GetSystem() = 'windows') then begin if (FileExists(Name)) then begin result:= true; end; end else begin if ((FileExists(Name)) or (ReadFile(Name) <> '')) then begin result:= true; end; end;end;function GetLines(const PATH: string): integer;var i, x: integer; s, FILE: string;begin i:= 1; if (DoesFileExist(PATH)) then begin FILE:= ReadFile(PATH); while (i <= length(FILE)) do begin s:= Copy(FILE, i, 2); if (s = #13#10) then begin Inc(Result, 1); Inc(i, 2); end else begin Inc(i, 1); end; end; end;end;
Code: [Select]function GetLines(FileDir: string): integer;var i,lines: integer; file: string;begin Lines := 0; file :=ReadFile(Filedir); For i := 1 to 1000 do begin If GetPiece(file,chr(13) + chr(10),i) <> '' then begin lines := i+1; end else break; end; Result := lines;end;Okay fully working scriptthere is just one problem.. if there are whitespaces in the file, it stopsAnyone, how could i fix that?
Would be a bit more elegant solution then checking every character.
function StringOccurs(needle, haystack: string): integer;begin while strpos(needle,haystack) <> 0 do begin Result := Result + 1; haystack := copy(haystack,strpos(needle,haystack)+1,length(haystack)); end;end;function CountLines(Filename: string): integer;begin Result := StringOccurs(chr(13)+chr(10), ReadFile(Filename));end;
I asume you mean a white line. That's pretty logical. You're checking everything between two newline chars. A white line is 'nothing' between two newline chars, so your for-loop will break.Im not sure if it will work, but you could try out something with chr(3) wich should be the ascii number for end-of-text.Would be a bit more elegant solution then checking every character.. still very nice function, curry