Results 1 to 8 of 8

Thread: [RESOLVED] Count # of lines in a file?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Posts
    259

    [RESOLVED] Count # of lines in a file?

    I am making a game, and info is stored in a text file. I need to know how many lines are in the text file, and it will never be a "set number".

    Is there anyway to quickly count the lines? Currently, I am reading the file and increasing a counter, and then reading the file again for the data. Another option I thought about was to write the number of lines to the first line in the file (-1 of course... since you don't want that piece of info).

    Any more information or suggestions would help!

    Thanks
    Last edited by kandieman101; Nov 3rd, 2002 at 02:30 PM.
    SonicMailer Pro
    The best mailing list manager has just gotten better!
    Click here for a full list of features!

  2. #2
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Well

    Basic textfile readin example :

    VB Code:
    1. Open SFILE For Input As #1
    2.                    
    3.     Dim STRING1 As String, STRING2 As String
    4.                                
    5.     Do Until EOF(1) = True
    6.  
    7.         Input #1, STRING1, STRING2
    8.                    
    9.         Msgbox String1 & " - " & String2
    10.                    
    11.     Loop
    12.                                
    13.     Close #1
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  3. #3
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046
    or you can read the file into one big string and:

    intNumberOfLines = Ubound(Split(strFileText,vbcrlf))+1

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Posts
    259
    Which is the fastest way?
    SonicMailer Pro
    The best mailing list manager has just gotten better!
    Click here for a full list of features!

  5. #5
    Fanatic Member scr0p's Avatar
    Join Date
    Oct 2002
    Location
    VA
    Posts
    720
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4. LineCount (Text1.Text)
    5. End Sub
    6.  
    7. Public Function LineCount(TheBody As String) As Long
    8.     Dim TotalChars As Long
    9.     Dim Letter, I
    10.     TotalChars = Len(TheBody)
    11.     LineCount = 0
    12.    
    13.     For I = 1 To TotalChars
    14.         Letter = Mid(TheBody, I, 1) 'This goes through every letter in TheBody
    15.         If Letter = Chr(13) Then
    16.         LineCount = LineCount + 1
    17.         I = I + 1 'This skips chr(10) which always follows chr(13)
    18.         End If
    19.     Next I
    20.  
    21.     MsgBox (LineCount)
    22. End Function
    asdf

  6. #6
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046
    Originally posted by kandieman101
    Which is the fastest way?
    ive got to think using the ubound(split) is the fastest

  7. #7
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    VB Code:
    1. 'function:
    2. Private Function NumberofLines(strFileName As String) As Integer
    3. Dim f As Integer
    4.  
    5.   f = FreeFile
    6.   Open strFileName For Input As #f
    7.     NumberofLines = UBound(Split(Input(LOF(1), 1), vbCrLf)) + 1
    8.   Close #f
    9. End Function
    10.  
    11. 'example use:
    12. Private Sub Command1_Click()
    13.   MsgBox NumberofLines("C:\file.txt")
    14. End Sub
    My evil laugh has a squeak in it.

    kristopherwilson.com

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Posts
    259
    Thanks Hobo.
    SonicMailer Pro
    The best mailing list manager has just gotten better!
    Click here for a full list of features!

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