Results 1 to 20 of 20

Thread: How to get the no. of lines on a file?

  1. #1

    Thread Starter
    Fanatic Member noielen's Avatar
    Join Date
    Nov 2005
    Location
    Cebu, Phil.
    Posts
    680

    Arrow How to get the no. of lines on a file?

    hey!
    somebody can help me on how to get the no. of lines on a file in vb6? tnx in advance...
    noister
    <advertising link removed by moderator>

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: How to get the no. of lines on a file?

    You need to read the content of the file and split it up after each line break.
    VB Code:
    1. Public Function GetLineCount(ByVal sFileName As String) As Long
    2.     Dim hFile As Integer
    3.     Dim sLines() As String
    4.     hFile = FreeFile
    5.     Open sFileName For Binary Access Read As #hFile
    6.         sLines = Split(Input(LOF(hFile), hFile), vbCrLf)
    7.     Close #hFile
    8.     'only check if the last line is blank
    9.     If Len(sLines(UBound(sLines))) = 0 Then
    10.         GetLineCount = UBound(sLines)
    11.     Else
    12.         GetLineCount = UBound(sLines) + 1
    13.     End If
    14. End Function

  3. #3
    Frenzied Member
    Join Date
    May 2003
    Location
    Sydney
    Posts
    1,123

    Re: How to get the no. of lines on a file?

    not plagiarism but a separate way of doing it
    VB Code:
    1. Public Function GetLineCount(ByVal sFileName As String) As Long
    2.     Dim hFile As Integer
    3.     Dim sLines As String
    4.     GetLineCount=0
    5.     hFile = FreeFile
    6.     Open sFileName For Input As #hFile
    7.     Do While Not EOF(hFile)
    8.         Input #hFile, sLines
    9.         If Len(sLines) > 0 Then
    10.             GetLineCount = GetLineCount + 1 'lines are not counted if blank
    11.         End If
    12.     Loop
    13.     Close #hFile
    14. End Function

  4. #4
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: How to get the no. of lines on a file?

    I personally think it's wrong to not count any of the empty lines in a text document. Since empty lines are often used to separate paragraphs from each other and in my humble opinion an empty line is still a line. In my example I didn't count the very last line if it was empty since the last line can end with a new line character and in that case shouldn't normally be counted. However empty lines inside the document should be counted.

    Apart from that mebhas code will use up less memory when it runs compared to mine (the memory will however be released when the function returns) but it will also be much slower especially on a large file since it reads only one line at the time. So it all depends on how you want to handle it. Both versions will work.

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

    Re: How to get the no. of lines on a file?

    My 2 cents.

    VB Code:
    1. Dim hFile As Long: hFile = FreeFile()
    2. Dim buffer() As Byte
    3.  
    4. Open <filename> For Binary Access Read Lock Write As #hFile
    5. ReDim buffer(LOF(hFile))
    6. Get #hFile, , buffer
    7. Close #hFile
    8.  
    9. Dim lineCount As Long, i As Long
    10. Const DELIMITER = ChrW$(10)
    11.  
    12. For i = 0 To Ubound(buffer) Step 2
    13.     lineCount = lineCount - (buffer(i) = DELIMITER)
    14. Next i

    Not sure if the Get -> buffer part will work since I need to reinstall VB.

  6. #6
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: How to get the no. of lines on a file?

    LOL, pena that will give you a negative line count. Congrats to the light green gem

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

    Re: How to get the no. of lines on a file?

    Cheers

    And no, it won't, because if it finds a delimiter it will subtract True which is -1

  8. #8
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: How to get the no. of lines on a file?

    Quote Originally Posted by Joacim Andersson
    LOL, pena that will give you a negative line count. Congrats to the light green gem
    Nope, I think that would work fine..
    if buffer(i) = DELIMITER:
    lineCount - (buffer(i) = DELIMITER) =
    lineCount - (True) =
    lineCount - (-1) = lineCount +1

    if buffer(i) <> DELIMITER:
    lineCount - (False) =
    lineCount - 0 = lineCount

    Adding lines when find delimiter

  9. #9
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: How to get the no. of lines on a file?

    Quote Originally Posted by penagate
    Cheers

    And no, it won't, because if it finds a delimiter it will subtract True which is -1
    That's correct, my bad

  10. #10

    Thread Starter
    Fanatic Member noielen's Avatar
    Join Date
    Nov 2005
    Location
    Cebu, Phil.
    Posts
    680

    Re: How to get the no. of lines on a file?

    tnx to all, actually i already got my own code... but tnx you add my knowledge...

  11. #11
    Fanatic Member lerroux's Avatar
    Join Date
    Nov 2005
    Location
    Welcome to the darkside... we have cookies
    Posts
    646

    Re: How to get the no. of lines on a file?

    noielen....

    how did you do it?
    can you post your code?
    WARNING: Excessive coding is dangerous to your health... if symptoms persist insult your doctor...

  12. #12
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: How to get the no. of lines on a file?

    I couldn't resist, here's a shorter one.
    VB Code:
    1. Public Function GetLineCount(ByVal sFileName As String) As Long
    2.     Dim hFF As Integer
    3.     Dim sFile As String
    4.    
    5.     hFF = FreeFile
    6.     Open sFileName For Binary As #hFF
    7.         sFile = Input(LOF(1), #hFF)
    8.     Close #hFF
    9.     If Len(sFile) Then
    10.         GetLineCount = (Len(sFile) - Len(Replace$(sFile, Chr$(13), vbNullString))) + 1
    11.     End If
    12. End Function

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

    Re: How to get the no. of lines on a file?

    Replace is slow. Looping and counting is the simplest and therefore quickest operation

  14. #14
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: How to get the no. of lines on a file?

    I don't understand why you use Step 2 in the loop in your example pena. If the file is a normal ASCII text file the vbLf (which is a constant you could have used just as well as declaring your own ) could of course be at any position in the file.

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

    Re: How to get the no. of lines on a file?

    ASCII yeah. I was thinking of Unicode.

    Also, the delimiter should just be 10 instead of Chr(10) since we are comparing byte values.

  16. #16
    Frenzied Member Andrew G's Avatar
    Join Date
    Nov 2005
    Location
    Sydney
    Posts
    1,587

    Re: How to get the no. of lines on a file?

    VB Code:
    1. Dim strdata As String
    2. Open App.Path & "\filename.txt" For Input As #1
    3. numlines = UBound(Split(Input(LOF(1), #1), vbCrLf)) + 1
    4. Close #1
    Last edited by Andrew G; Dec 21st, 2005 at 05:41 AM.

  17. #17
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: How to get the no. of lines on a file?

    That's the same code I used above but without the test for the last empty line. Just because you combine function calls into one line doesn't execute them faster, it just makes it harder to read and understand.

  18. #18
    Frenzied Member Andrew G's Avatar
    Join Date
    Nov 2005
    Location
    Sydney
    Posts
    1,587

    Re: How to get the no. of lines on a file?

    Oops! Sorry , i should have read it more closely, it would have saved me alot of time trying to find out how input works.

  19. #19
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: How to get the no. of lines on a file?

    Well, all suggestions above are valid ways for checking the number of lines in a text file (with the corrections already mentioned to get Pena's code to work correctly). Many of them is discussing the speed issue, in my opinion many developers go into this issue to deep when there is no reason to do so. The bottle neck when it comes to speed for this code is the disk access (reading the file). This is what takes some time and you shouldn't access the disk more then you need to, so in most cases it would be best to read the whole content of the file in one go. This is the case in both my code as well as the code pena showed (and the code Andrew used as well).

    It is probably (most certain, depending on how long each line is) faster to use a loop in a byte array, like Pena did, then to call the Split function, as I did, however you must also consider the readability of the code. If you haven't written clearly comments that describes what you do it is a bit harder to understand what the loop is actually doing since you must first figure out what 10 in a byte actually represents. For me clear code that is easy to follow might be better to use then to earn a couple of milliseconds. Especially in code like this that probably wont be used so many times in the program, it might matter if you run it a million times but in that case we are back to the issue that it takes a long time to read a million files so if it takes one second longer or shorter normally doesn't really matter much for the user anyway. Normally in that case you would probably want to slow the code down even further by showing some sort of progress to the user so he/she doesn't think the program have stopped responding.

    I'm not trying, with this comment, to rant down on any of the code examples or to claim that my suggestion is the best. I only wanted to put this in some perspective. The same goes with trying to squeeze the code down to as few lines as possible. It's nice when you can use a one liner to do something as long as that one liner is clear and easy to understand. But IMHO combining many function calls on one line just to save some typing is rarely the best approach. I remember a few years back when it was very popular amoung many C developers to write as much as possible on one line, to actually make the code cryptic was the goal. Which is just stupid, making code cryptic should never be the goal, since those that have access to the source code should be able to read and understand it.
    Code:
    m=((a>b)?(a>c)?a:c:(b>c)?b:c)
    The above C code might run slightly faster then if you would break it up on a couple of lines, but even if you know C it will take you a while before you figure out what it does.

    It's also interesting that a simple question as this has become such a long thread.

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

    Re: How to get the no. of lines on a file?

    Code:
    if (a > b) {
        if (a > c)
            m = a;
        else
            m = c;
    } else {
        if (b > c)
            m = b;
        else
            m = c;
    }
    ...there, I had to

    But I agree you shouldn't make things unreadable just for the sake of it. Only if it is a time-critical operation, then you can try different cryptic ways of making it faster, as long as you comment it

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