CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: File line Count

  1. #1
    Join Date
    Aug 2000
    Posts
    60

    File line Count

    Hi,
    I opened a file using
    open filename for input as #1

    How do i know how many line is in that file?
    Does anyone know what API to use to count the number of lines in the file?

    Thank you so much in advance.

    Sue


  2. #2
    Join Date
    Mar 1999
    Location
    Nepal
    Posts
    540

    Re: File line Count

    This is not an efficient method but should work


    dim OneLine as string, LineCnt as integer
    open filename for input as #1
    LineCnt = 0
    do until eof(1)
    line input #1, OneLine
    LineCnt = LineCnt + 1
    loop
    close #1
    msgbox LineCnt





  3. #3
    Join Date
    Aug 1999
    Location
    Bangalore, INDIA
    Posts
    70

    Re: File line Count

    Try this. this works much faster.

    Dim strFileName as string
    strFileName = "c:\temp\resy2000.log"
    Dim strHuge as string
    Dim LineCnt as Long
    Dim ln as Long
    Dim strFileContent() as string
    Dim iFile as Integer

    iFile = FreeFile
    Open strFileName for input as #iFile
    ln = FileLen(strFileName) 'get the lenth of the file
    strHuge = Space(ln) 'Required
    strHuge = input(ln, iFile) 'Read all the data into a string
    Close #iFile 'close the file
    strFileContent = Split(strHuge, vbCrLf)
    LineCnt = UBound(strFileContent) ' this will have the number of lines in the file
    'strFileContent will have the contents of the file as an array of string.





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

Featured