Page 1 of 1
[03 Nov 2009] Number of lines in a file
Posted: Sat Jun 13, 2020 5:32 pm
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
[04 Nov 2009] Re: Number of lines in a file
Posted: Sat Jun 13, 2020 5:32 pm
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)
[04 Nov 2009] Re: Number of lines in a file
Posted: Sat Jun 13, 2020 5:32 pm
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.)
[04 Nov 2009] Re : Re: Number of lines in a file
Posted: Sat Jun 13, 2020 5:32 pm
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 ~
[04 Nov 2009] Re: Re: Number of lines in a file
Posted: Sat Jun 13, 2020 5:32 pm
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