Page 1 of 1

How to get the number of lines in the file quickly!

Posted: Thu Oct 02, 2003 2:44 pm
by sonic3d
Sorry for my poor English!

Anyone can show me a way to get how many lines in a file quickly.
I'm using a stupid solution:

Code: Select all

      If ReadFile(0, GetGadgetText(#TextFilename))
        k.l=0
        While Eof(0)=0
          InfoString$ =ReadString()
          k.l=k.l+1
        Wend
        SetGadgetText(#TextBX, Str(k.l))
        Debug "File opened successfully"
      Else
        Debug "Faint!It's impossible:P"
      EndIf
Thanks!

Posted: Thu Oct 02, 2003 3:20 pm
by GedB
Thats about the best way to do it. If you are reading in a file with irregular file lengths then you have no choice but to read the whole file.

The only other option is to use LOF() and divide by an approximate line length, but this will only give an estimate.

Posted: Thu Oct 02, 2003 3:24 pm
by freak
Have a look at the FastFileText library by Rings:
http://www.purearea.net/pb/download/use ... etext3.zip

The FastOpenTextFile( Filename ) in there allready gives the number of lines
as a result.

Timo

Posted: Thu Oct 02, 2003 4:36 pm
by sonic3d
Thank you very much indeed!
It's really fast! :D