Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Get number of lines in a text file

Author
Message
BriereAnthony
13
Years of Service
User Offline
Joined: 20th Feb 2011
Location:
Posted: 19th Apr 2011 01:06
I guess the title says it all. I'd like to know if there is a function that will return the amount of lines in a text file.

Thank you for your help!
Ashingda 27
16
Years of Service
User Offline
Joined: 15th Feb 2008
Location:
Posted: 19th Apr 2011 01:47
You can manually count it yourself I suppose. Open the file and read each line as a string and check the length of it with LEN(string$) until the end of file and add it up.

BriereAnthony
13
Years of Service
User Offline
Joined: 20th Feb 2011
Location:
Posted: 19th Apr 2011 02:33
Yea that's what I'm actually doing. But I was just wondering if there was a built-in function for that.

Thanks for answering!
Agent
20
Years of Service
User Offline
Joined: 7th Sep 2004
Location: Sydney, Australia
Posted: 19th Apr 2011 16:31
I realise Ashingda has already answered this question, but I like to corroborate so that the original poster has some certainty that he's been given the right info.

I know of no way to determine the number of lines in a file beyond reading them all until EOF and counting them as you go, unless the first entry in the file is its length (which is a technique I sometimes use with my game data files).

BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 20th Apr 2011 00:43
If you are writing the file yourself and you need it to be efficient then you can include the line count in the file as a value.

IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 20th Apr 2011 00:56
Quote: "I'd like to know if there is a function that will return the amount of lines in a text file."

Why? What are you going to use that count for?

BriereAnthony
13
Years of Service
User Offline
Joined: 20th Feb 2011
Location:
Posted: 20th Apr 2011 01:18
Quote: "Why? What are you going to use that count for?"


I'm creating a text-based rpg and am storing items, quests, events, etc in .txt files so that everyone can add/remove/modify them. I need to initialise an array with the correct size so I believe getting the number of lines would do it correctly (as every weapon/item takes one line)
Ashingda 27
16
Years of Service
User Offline
Joined: 15th Feb 2008
Location:
Posted: 20th Apr 2011 02:24
A technique I use to use was to save the amount of elements before saving the element data. When reading, it'll first read the amount of elements first and loop it just that much.

But in my opinion it's just best to save the whole array even if it's filled with a bunch of zeros. Then when you read it just read the exact same way you saved it.

Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 21st Apr 2011 17:33
You could just create the array with nothing in it and add elements to the array as you need them with ARRAY INSERT AT BOTTOM. That way there's no need to even know how many lines are in the file before starting. Once the file is fully loaded then you'll know the size of the array if you need that for any randomization limits.



Agent
20
Years of Service
User Offline
Joined: 7th Sep 2004
Location: Sydney, Australia
Posted: 22nd Apr 2011 20:52
Yes. The array will dynamically redimension itself as you go if you use Grog's technique. You can declare it with zero size, as he has done in his example code, and calling ARRAY INSERT AT BOTTOM automatically increases its declared size each time.

Sounds like that'll be the best path for you, if you're shooting for efficiency (even though I generally don't like automated processes like this - just a personal preference).

BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 23rd Apr 2011 15:37
Using arrays like this isn't very efficient. Everytime you change the array size, the entire array is moved to another part of memory. It would be better to Dim the array to increase it by several elements at a time. Using Dim retains the data as you resize it. e.g

Dim MyArray(100)
... do stuff
...run out of elements
Dim Myarray(200)
...carry on


Login to post a reply

Server time is: 2024-11-16 19:16:19
Your offset time is: 2024-11-16 19:16:19