BBS: Inland Empire Archive Date: 04-17-92 (21:30) Number: 91 From: STEVE HALKO Refer#: NONE To: ZACK JONES Recvd: NO Subj: File size Conf: (2) Quik_Bas
In a msg of <15 Apr 92>, Zack Jones mused:
ZJ> I'm trying to find the fastest way for me to find the length of a
ZJ> file. I know LOF shows the size in bytes. What I want to know is how
ZJ> many lines there are in an ASCII text file that I open. I can count
ZJ> the number of lines as I read the file using a line input statement,
ZJ> but is there a faster way than using a do while loop to read the file
ZJ> and count it line by line? All help is greatly appreciated.
The fastest way is to read the file as binary, and read it in large
chunks. If your text file is less than 32K, you can read it in in one
large chunk:
'LINECNT.BAS
DEFINT A-Z
INPUT "Filename: "; FileName$
FileNr = FREEFILE
OPEN FileName$ FOR BINARY AS FileNr
CrLf$ = CHR$(13) + CHR$(10)
Length = LOF(FileNr)
Buffer$ = SPACE$(Length) 'Create a buffer the length of the file
GET FileNr, , Buffer$ 'Read entire file at once
CLOSE FileNr
LineCount = 0
Start = 1
'Now go thru and find CR/LFs:
DO
Cr = INSTR(Start, Buffer$, CrLf$) 'Find the next CR/LF
IF Cr THEN 'If CR/LF found
LineCount = LineCount + 1 'Increment count
Start = Cr + 2 'Skip over CR/LF to start
'search for next CR/LF
ELSE
EXIT DO 'If no more CR/LF
END IF
LOOP WHILE Start < Length
PRINT "There are"; LineCount; " lines in "; FileName$
END
If your text file is larger than 32K, then just read as many chunks as
you need, and process each chunk separately.
* SLMR 2.1a * Common sense is not so common.
--- DB B1065/002487
* Origin: Gulf Coast BBS -QuickSHARE #2- (904)563-2547 HST/V.32bis (1:365/12)

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