[03 Nov 2009] Number of lines in a file

Contains all messages from the Hollywood mailing list between 01/2006 and 08/2012
Locked
User avatar
Tarzin
Posts: 112
Joined: Mon Feb 15, 2010 11:46 am
Location: Dunkerque / FRANCE
Contact:

[03 Nov 2009] Number of lines in a file

Post by Tarzin »

Note: This is an archived post that was originally sent to the Hollywood mailing list on Tue, 03 Nov 2009 20:20:44 +0100

Hello,

How could I optimize this code?

Code: Select all

OpenFile(1,fichier$, #MODE_READ)
count=0
While not Eof(1)
count=count+1  Seek(1,count)
test=ReadChr(1)
if test=13 count1=count1+1 EndIf
Wend
Print (count1,"Games included in database")
The aim is to count number of lines included in an ascii file. I read all caracters of the file and count all returns (with chr(13) code) But it's very long (my file has 1714 lines)

Thanks for advance!

Eric
A500 / A600 / A1200 / SAM440
WinUAE OS3.9 (AmiKit) / OS4.1FE (FlowerPot)
---
https://twitter.com/TarzinCDK
PEB
Posts: 576
Joined: Sun Feb 21, 2010 1:28 am

[04 Nov 2009] Re: Number of lines in a file

Post by PEB »

Note: This is an archived post that was originally sent to the Hollywood mailing list on Wed, 04 Nov 2009 00:01:41 -0000

You could try this code:

Code: Select all

file$=FileRequest("Select Background Image", "")
OpenFile(1, file$)
data$=ReadString(1, FileLength(1))
CloseFile(1)
table, LineNum=SplitStr(data$, "\n")
DebugPrint("Number of Lines:", LineNum)
PEB
Posts: 576
Joined: Sun Feb 21, 2010 1:28 am

[04 Nov 2009] Re: Number of lines in a file

Post by PEB »

Note: This is an archived post that was originally sent to the Hollywood mailing list on Wed, 04 Nov 2009 00:07:42 -0000

The first line should be changed to this: file$=FileRequest("Select File", "")

(That's what I get for copying and pasting code from a different example.)
critonsgate
Posts: 14
Joined: Thu Mar 04, 2010 6:46 pm

[04 Nov 2009] Re : Re: Number of lines in a file

Post by critonsgate »

Note: This is an archived post that was originally sent to the Hollywood mailing list on Wed, 4 Nov 2009 07:46:53 +0000 (GMT)

It's the best way to read the number of lines. You're reading only one string, after you're cutting it with separator "/n".

The performance is better. Thanks for the tip !

Excuse me for my bad english. Regards. ~ CritonSgate ~
User avatar
Tarzin
Posts: 112
Joined: Mon Feb 15, 2010 11:46 am
Location: Dunkerque / FRANCE
Contact:

[04 Nov 2009] Re: Re: Number of lines in a file

Post by Tarzin »

Note: This is an archived post that was originally sent to the Hollywood mailing list on Wed, 04 Nov 2009 12:30:15 +0100

Hello rev

Whaaaaouuu! Working fine and really quickly. Thanks a lot!

Eric
A500 / A600 / A1200 / SAM440
WinUAE OS3.9 (AmiKit) / OS4.1FE (FlowerPot)
---
https://twitter.com/TarzinCDK
Locked