Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Number of lines in a ascii file 4

Status
Not open for further replies.

FoxWing

Programmer
Dec 20, 2004
44
GB
Hi,

can anyone advice me what function can i use to get the number of lines in a txt file ?


Thanks in advance.
 
Hi FoxWing,

This works for files with less than 65000 lines:
? alines(aDummy,filetostr("d:\yourfile.txt"))

Maybe even easier is
? occurs(filetostr("d:\yourfile.txt"),chr(13))

But that would not count a line without a carriage return at the end, so would perhaps miss by 1, but not always.

Bye, Olaf.
 
You may want to verify what code sequence you have there for line breaks. I've seen CHR(13), CHR(10), and combined CHR(13)+CHR(10), but never in reverse order CHR(10)+CHR(13). I mention this in case you remove only one of 2 characters.

dbMark
 
And finally... one to just obfuscate the obvious, but
counts lines regardless of the termination character.
i.e. lf's or cr's

( note: not fully fool-proof ;-) )

Code:
function GetLineCount
  lparam cFile
  local nBytesCR, nBytesLF, nBytes, cLines
  nBytes = 0
  if vartype(cFile) == "C" .and. file(cFile)
    cLines   = filetostr(cFile)
    nBytesCR = occurs(chr(13),cLines)
    nBytesLF = occurs(chr(10),cLines)
    nBytes   = iif(inlist(right(cLines,1),chr(10),chr(13)),0,1)
    nBytes   = max(nBytesCR,nBytesLF) - nBytes
  endif
  return nBytes
endfunc
 
Thanks for the advices from all of you (Dave,Olaf,dbMark,darrellblack). Because of you, i got it working now.

Cheers.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top