Author Topic: Count lines in a text file  (Read 424 times)

MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 786
    • View Profile
Count lines in a text file
« on: July 13, 2020, 12:35:34 AM »
It is sometimes helpful to know how many lines of text exists in a plain text file.
The most common way is to read each line until the end of file is reached.

On larger text files, that may take more time than is tolerable, so another way
is to read the text file in binary mode and grab bigger chunks of it.  That's what
the code below does.  And instead of counting lines, it uses the TALLY function
to count ASCII code 10 (linefeed), common in text files running on Windows,
Unix, Linux, MacOS, etc.

Using "BC.BAS" for our test file in the example below, instead of accessing the disk
over 32,000 times, we only access it about 70 times by reading 16k at a time.  The
16k buffer was chosen arbitrarily - you could increase it but there will come a point
where the TALLY function slows things down when the buffer is too large.

Anyway ... it's just one more cool tool for your toolbox.


Code: [Select]


PRINT FileLineCount ("BC.bas")



FUNCTION FileLineCount (Filename$) AS LONG
  LOCAL FF AS FILE       ' local file handle that we'll use
  LOCAL Linefeeds        ' accumulate the line feeds in the text file
  LOCAL BUFSIZE = 16384  ' 16k buffer
  LOCAL Buff$ * BUFSIZE

  IF EXIST(Filename$) = 0 THEN EXIT FUNCTION

  OPEN Filename$ FOR BINARY AS FF

  WHILE NOT EOF(FF)
    GET$ FF, Buff$,BUFSIZE
    Linefeeds += TALLY (Buff$,LF$)
    memset (Buff$,0,BUFSIZE)   'memset is a C lib function that I use to zero the buffer each pass
  WEND

  CLOSE FF
  FUNCTION = Linefeeds
END FUNCTION

« Last Edit: July 13, 2020, 10:37:35 AM by MrBcx »