Results 1 to 7 of 7

Thread: Counting Lines in a file [VB]

  1. #1

    Thread Starter
    Hyperactive Member Datacide's Avatar
    Join Date
    Jun 2005
    Posts
    309

    Counting Lines in a file [VB]

    Is there another way (faster way) to count the number of lines in a file instead of doing it this way:
    VB Code:
    1. Open filename for Input As #1
    2.   Do While Not Eof(1)
    3.     Line Input #1, tmp
    4.     c = c + 1
    5.   Loop
    6. close #1
    7. Open filename for Input As #1
    8.   'do stuff
    9. close #1
    PHP in your FACE!

  2. #2
    Fanatic Member VBAhack's Avatar
    Join Date
    Dec 2004
    Location
    Sector 000
    Posts
    617

    Re: Counting Lines in a file [VB]

    I presume you mean a text file with lines separated by CR or LF? If so, I asked a similar question (http://www.vbforums.com/showthread.php?t=333717) and got this reply (haven't tried it though):

    Read the whole text file into memory (in Binary) in one shot. Split the file on LF's and take the Ubounds of the resulting array. This will give you the total lines in a file quite quickly.
    VBAhack

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Counting Lines in a file [VB]

    An example of that, and an alternative way to count (not sure which is faster):
    VB Code:
    1. Dim iFileNo As Integer
    2. Dim sFile As String
    3.   iFileNo = FreeFile      'Load entire file into string
    4.   Open filename For Binary As #iFileNo
    5.   sFile = Space(LOF(iFileNo))
    6.   Get #iFileNo, , sFile
    7.   Close #iFileNo
    8.  
    9. MsgBox "file loaded"
    10.  
    11. 'method 1 (split)
    12. Dim vSplit as Variant
    13.   vSplit = Split(sFile,vbCrLf)
    14.   MsgBox "Number of lines = " & UBound(vSplit)+1
    15.  
    16. 'method 2 (replace)
    17.   MsgBox "Number of lines = " & (Len(sFile) - Len(Replace(sFile,vbCrLf,"")) \ 2
    18.  
    19.   Open filename for Input As #1
    20.     'do stuff
    21.   close #1
    Last edited by si_the_geek; Sep 15th, 2005 at 06:19 PM. Reason: oops.. see below!

  4. #4
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Counting Lines in a file [VB]

    VB Code:
    1. MsgBox "Number of lines = " & [b]U[/b]Bound(vSplit)+1



    Has someone helped you? Then you can Rate their helpful post.

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Counting Lines in a file [VB]

    Thanks, I've corrected it now.

  6. #6
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Counting Lines in a file [VB]

    Even quickier would be to open it in binary and reading it to a byte array instead of a string. It is faster to loop through a byte array counting the character codes than to split a string to string array. This technique would also allow easily to do block reading (ie. 2 GB file, read one MB at a time, count the line changes in the block, get the next block and count...).

    There are also other methods of reading a file in a quicker manner. I haven't tested them myself though.

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Counting Lines in a file [VB]

    Let the games begin.

    VB Code:
    1. Function CountLines( _
    2.     ByRef pszFilename As String, _
    3.     ByVal fUnicode As Boolean _
    4. ) As Long
    5.  
    6. Dim hFile       As Long
    7. Dim lFilelen    As Long
    8. Dim lpPos       As Long
    9. Dim chBuf()     As Byte
    10.  
    11. Dim lMax        As Long
    12. Dim i           As Long
    13. Dim lStep       As Long
    14.  
    15. Const CHUNKSIZE = 1024
    16.  
    17.     hFile = FreeFile()
    18.     Open pszFilename For Binary Lock Write As #hFile
    19.  
    20.     lFilelen = LOF(hFile)
    21.     ReDim chBuf(CHUNKSIZE)
    22.  
    23.     lStep = -fUnicode + 1
    24.  
    25.     Do While (lpPos < lFilelen)
    26.         If ((lFilelen - CHUNKSIZE) < lpPos) Then _
    27.             ReDim chBuf(lFilelen - lpPos)
    28.  
    29.         Get #hFile, lpPos + 1, chBuf
    30.  
    31.         lMax = UBound(chBuf)
    32.         For i = 0 To lMax Step lStep
    33.             If (chBuf(i) = 13) Then _
    34.                 CountLines = CountLines + 1
    35.         Next i
    36.  
    37.         lpPos = lpPos + CHUNKSIZE
    38.     Loop
    39.  
    40.     Close #hFile
    41.  
    42. End Function

    I haven't tried optimising it yet, but it's a start

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width