File Size

 BBS: Inland Empire Archive
Date: 04-16-92 (19:55)             Number: 174
From: PAUL LEONARD                 Refer#: NONE
  To: ZACK JONES                    Recvd: NO  
Subj: File Size                      Conf: (2) Quik_Bas
On or about <Apr 15 16:46>, Zack Jones (1:387/905.1) scribbled:

 ZJ> I'm trying to find the fastest way for me to find the length of a file.
 ZJ> I know LOF shows the size in bytes.  What I want to know is how many
 ZJ> lines there are in an ASCII text file that I open.  I
 ZJ> can count the number
 ZJ> of lines as I read the file using a line input statement, but is there a
 ZJ> faster way than using a do while loop to read the file and count it line
 ZJ> by line?  All help is greatly appreciated.

========================================================================
CONST recsize = 1024                                  'up to 32767
DIM rec AS STRING * recsize
delimiter$ = CHR$(13)

INPUT "File to read"; in$
OPEN in$ FOR RANDOM AS #1 LEN = recsize

DO UNTIL EOF(1)
  GET #1, , rec$
  DO
    delimiter% = INSTR(delimiter% + 1, rec$, delimiter$)
    IF delimiter% THEN lines% = lines% + 1
  LOOP WHILE delimiter%
LOOP

PRINT in$; " has"; lines%; "lines."
CLOSE 1
END
============================================================================

Not all that elegant, but it's faster than sequential
input.  Even with a small buffer like 1K (the 1024 record
size), on average it read a 68K text file (about 1500
lines) in about 0.5 seconds while sequential LINE INPUT
took about 3.3 seconds.

Later,
ptl


(I saw a great tagline today - "Every now and then an
innocent man is sent to the legislature.")

--- msged 2.07
 * Origin: PTL Pointwork (1:140/23)
Outer Court
Echo Basic Postings

Books at Amazon:

Back to BASIC: The History, Corruption, and Future of the Language

Hackers: Heroes of the Computer Revolution (including Tiny BASIC)

Go to: The Story of the Math Majors, Bridge Players, Engineers, Chess Wizards, Scientists and Iconoclasts who were the Hero Programmers of the Software Revolution

The Advent of the Algorithm: The Idea that Rules the World

Moths in the Machine: The Power and Perils of Programming

Mastering Visual Basic .NET