Lines in a File

 BBS: Inland Empire Archive
Date: 04-16-92 (09:06)             Number: 152
From: MATT HART                    Refer#: NONE
  To: ZACK JONES                    Recvd: NO  
Subj: Lines in a File                Conf: (2) Quik_Bas
 ZJ> is how many lines there are in an ASCII text file that I open.  I
 ZJ> can count the number of lines as I read the file using a line

' COUNTLNS.BAS  Matt Hart
'
' Counts the number of lines in a text file by
' counting occurrances of CR/LF.

    DEFINT A-Z
    DECLARE FUNCTION CountLines&(File$)

    F$ = LTRIM$(RTRIM$(COMMAND$))

    PRINT F$+" has";CountLines&(F$);"lines"
    END

FUNCTION CountLines&(File$)
    S$ = CHR$(13)+CHR$(10)
    F = FreeFile
    OPEN "B",F,File$
    L& = LOF(F)
    SeekPos = 0&
    CL& = 0&
    DO UNTIL L&<1&
        IF L& < 4096& THEN
            A$ = SPACE$(L&)
            SeekPos& = SeekPos& + L&
            L& = 0&
        ELSE
            A$ = SPACE$(4096)
            SeekPos& = SeekPos& + 4096&
            L& = L& - 4096&
        ENDIF
        GET F,,A$
        P=INSTR(A$,S$)
        DO UNTIL P = 0
            CL& = CL& + 1&
            P = P + 2
            P=INSTR(P,A$,S$)
        LOOP
        IF RIGHT$(S$,1) = CHR$(13) THEN
            L& = L& + 1
            SeekPos& = SeekPos& - 1&
            SEEK #F,SeekPos&
        ENDIF
    LOOP
    CLOSE F
    CountLines& = CL&
END FUNCTION

---
 * Origin: Midnight Micro!  V.32/REL  (918)451-3306 (1:170/600)
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