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.

DarkBASIC Discussion / reading text from a file (i.e. cfg file)

Author
Message
sicjoshsic
22
Years of Service
User Offline
Joined: 8th May 2003
Location: United Kingdom
Posted: 11th May 2003 20:59
how do you do it (in db classic, no enhancements)
ive looked at the read byte/read string etc. commands, but you can't specify where to start reading from.

what i am trying to do is like in 3d world editor, where it reads the screen settings/registration settings from a .cfg file
Dr DooMer
22
Years of Service
User Offline
Joined: 22nd Dec 2002
Location: United Kingdom
Posted: 11th May 2003 21:10 Edited at: 11th May 2003 21:13
Well, I've written a name generator that reads from a file; I'm going to be using it in my next game, but you're welcome to have a look at the source code if you like! Just poke the source button below to see it.

Anyway, what happens it that DB will look at each line in sequence, starting from the first. That means, if you want to read the third line, you've got to read the first two beforehand. Really, you should read from the config file right at the start and save everything into arrays or something, rather than read each value from a file every time it's needed.

There probably are ways of going straight to a certain point in a file, but I'm not well-versed enough in file I/O to know what they are.

Hope this helps!

"I am a living, thinking entity who was created in the sea of information."
sicjoshsic
22
Years of Service
User Offline
Joined: 8th May 2003
Location: United Kingdom
Posted: 11th May 2003 21:31
thanks, i'll mess about with that code for a while and see if i can get it to return integers (should be very simple, but nothing ever works out like planned )

BatVink
Moderator
22
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 11th May 2003 23:31
If you have no enhancements, my code behind the source button will do what you are looking for. The comments explain everything.

There are better ways, but you would need to be able to use DLLs (ti use TAToads plug-in).

Thanks in advance.
All the Best,
StevieVee
Ian T
22
Years of Service
User Offline
Joined: 12th Sep 2002
Location: Around
Posted: 12th May 2003 00:01
This simple function will read from a file and drop what it reads into an array. Filename$ is obviously the filename, Startline is the first line of the program it reads, Endline is the last line, and Startindex is where it should start placing the values in the array. It does translate the text to integers (floats actually, but...)

For example:


grabnum(data.txt,5,15,60)

will place lines 5 to 15 from data.txt into spaces 60 through 70 in the array 'array#'.

Make sure to edit the array called in the function to reflect what's in your code.

--Mouse

Famous Fighting Furball
Ian T
22
Years of Service
User Offline
Joined: 12th Sep 2002
Location: Around
Posted: 12th May 2003 00:02
Oh, sorry. That comment in the code says it takes strings-- I forgot to remove it while I was editing it for your post. As you can see, I use a slightly different version to get string data in what I'm working on .

--Mouse

Famous Fighting Furball
sicjoshsic
22
Years of Service
User Offline
Joined: 8th May 2003
Location: United Kingdom
Posted: 12th May 2003 12:44
thanks everyone!
i havn't used any of your code (exactly), but ive took Dr DooMer's code and altered it so that it simply scans upto the tag, reads the value into a string, then converts the string to a value.

to make it run faster ive also mad it so that it only scans what it needs to when it is called (using SELECT CASE) and then it stores all its info into an array in the main program.

thanks Dr DooMer, i'll include you in the credits of my game!
thanks everyone else for helping too

Dr DooMer
22
Years of Service
User Offline
Joined: 22nd Dec 2002
Location: United Kingdom
Posted: 12th May 2003 17:32
Thanks - I tidied that up a while ago for a code-snippet on a friend's website, which unfortunately became defunct. It's nice to know it's helping someone now!

"I am a living, thinking entity who was created in the sea of information."

Login to post a reply

Server time is: 2025-05-18 02:50:45
Your offset time is: 2025-05-18 02:50:45