|
-
Nov 3rd, 2002, 12:41 PM
#1
Thread Starter
Hyperactive Member
[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.
-
Nov 3rd, 2002, 12:44 PM
#2
PowerPoster
Well
Basic textfile readin example :
VB Code:
Open SFILE For Input As #1
Dim STRING1 As String, STRING2 As String
Do Until EOF(1) = True
Input #1, STRING1, STRING2
Msgbox String1 & " - " & String2
Loop
Close #1
Remaining quiet down here !!!
BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....
-
Nov 3rd, 2002, 12:47 PM
#3
PowerPoster
or you can read the file into one big string and:
intNumberOfLines = Ubound(Split(strFileText,vbcrlf))+1
-
Nov 3rd, 2002, 12:53 PM
#4
Thread Starter
Hyperactive Member
Which is the fastest way?
-
Nov 3rd, 2002, 02:01 PM
#5
Fanatic Member
VB Code:
Option Explicit
Private Sub Command1_Click()
LineCount (Text1.Text)
End Sub
Public Function LineCount(TheBody As String) As Long
Dim TotalChars As Long
Dim Letter, I
TotalChars = Len(TheBody)
LineCount = 0
For I = 1 To TotalChars
Letter = Mid(TheBody, I, 1) 'This goes through every letter in TheBody
If Letter = Chr(13) Then
LineCount = LineCount + 1
I = I + 1 'This skips chr(10) which always follows chr(13)
End If
Next I
MsgBox (LineCount)
End Function
-
Nov 3rd, 2002, 02:04 PM
#6
PowerPoster
Originally posted by kandieman101
Which is the fastest way?
ive got to think using the ubound(split) is the fastest
-
Nov 3rd, 2002, 02:12 PM
#7
Stuck in the 80s
VB Code:
'function:
Private Function NumberofLines(strFileName As String) As Integer
Dim f As Integer
f = FreeFile
Open strFileName For Input As #f
NumberofLines = UBound(Split(Input(LOF(1), 1), vbCrLf)) + 1
Close #f
End Function
'example use:
Private Sub Command1_Click()
MsgBox NumberofLines("C:\file.txt")
End Sub
-
Nov 3rd, 2002, 02:28 PM
#8
Thread Starter
Hyperactive Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|