Count Lines Of Text In Txt File

hi i have text file ex. (sample.text) and i need to count lines in file and disply in memo .


I have small examp but this not work any sugestion.

function linecount(filename: string): integer;
var
f: textfile;
begin
assignfile(f,filename);
reset(f);
result:= 0;
while not eof(f) do
begin
readln(f);
inc(result);
end;
closefile(f);
end;

Thanks for help.

Recommended Answers

All 4 Replies

Please use code tags when posting on daniweb:

[code=delphi] ...code here...

[/code]

Next I ran your code and it seems to work OK for giving you the line count. What problem are you experiencing? Are you reading in a file with unix style line breaks instead of windows? Please elaborate on what is not working.

its not give my any count i read in window style.
plise tell my your working combination on my code.

paste example code

thanks

procedure TForm1.Button1Click(Sender: TObject);
var result:Integer;
    s:string;            {!}
function linecount(filename: string): integer;
 var f: textfile;
begin
  assignfile(f,filename);
  reset(f);
  result:= 0;
  while not eof(f) do begin
    readln(f);
    inc(result);
  end;
  closefile(f);
  linecount:=result;   {!}
end;
begin
memo1.Clear;   {!}
Str(linecount('C:\sample.txt'),s);   {!}
memo1.Lines.Add(s);   {!}

end;

"Now I see where I'm wrong"
Big Thanks to FlamingClaw

real professional answer

and Scott Knake.
Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.