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

Just starting out? Need help? Post your questions and find answers here.
sonic3d
New User
New User
Posts: 9
Joined: Thu Oct 02, 2003 2:36 pm

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

Post 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!
User avatar
GedB
Addict
Addict
Posts: 1313
Joined: Fri May 16, 2003 3:47 pm
Location: England
Contact:

Post 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.
freak
PureBasic Team
PureBasic Team
Posts: 5948
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post 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
quidquid Latine dictum sit altum videtur
sonic3d
New User
New User
Posts: 9
Joined: Thu Oct 02, 2003 2:36 pm

Post by sonic3d »

Thank you very much indeed!
It's really fast! :D
Post Reply