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 txt files

Author
Message
Bob_42
19
Years of Service
User Offline
Joined: 15th Sep 2006
Location: Guernsey
Posted: 21st Sep 2006 20:43
ok, i've searched through the forums high and low, and i still haven't found exactly what i am looking for, i found out how to write to a txt file and read it, but it only seems to read the first line...

all i really want, is the program to read the .txt file for a list of settings, for example;

speed = 10
range = 100
model = model.x

how would i so something like this?
Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 22nd Sep 2006 01:46
Hello There,

You have to loop through the file until you reach it's end.

Take a look... replace f:\ with whatever directory path you want



Enjoy your day.
Ginga
20
Years of Service
User Offline
Joined: 3rd Jul 2006
Location: Dorset, England
Posted: 22nd Sep 2006 16:44
Latch has got it right, but one thing i norm do, is add a number at the very top of the file for how many times u have to read sumit in. the text document (gamestats.txt) would look sumit like EG:

3
speed = 10
range = 100
model = model.

say the file is already in the same directory, the code to read this in would b quite simple

Bob_42
19
Years of Service
User Offline
Joined: 15th Sep 2006
Location: Guernsey
Posted: 22nd Sep 2006 19:58
how would i make it so that when the text is read that, (in my example) speed = 10, rather than info(1) = "speed = 10". Because i want to add it to my game so that i can say move object 1,speed.
Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 22nd Sep 2006 20:38 Edited at: 23rd Sep 2006 22:55
[EDIT] I must've sent this late, it was supposed to appear right after Ginga's post - It's not a response to Bob's second post.

[Old]
@ Ginga

That's really clever and so simple!

@ Bob

With Ginga's suggestion, you could have sections of different data within your file. Each section identified by a count to indicate how many lines of info are included for that section.

Let's say you have two sections of three lines of info. The first line has the count of lines + 1 (the plus one indicates that there is another section following this one and to get the next count variable when this one is one it's last count).

4
speed = 10
range = 100
model = model
3
score = 100
health = 50
rate = 5

You can get as fancy as as you want - using multidimensioned arrays to get at specifc indexes of data, etc. etc.

Enjoy your day.
Bob_42
19
Years of Service
User Offline
Joined: 15th Sep 2006
Location: Guernsey
Posted: 22nd Sep 2006 22:19
but that doesn't help, i want to make each line into a variable, so speed will equal 10, range will equal 100. So i can then refer to this when controlling the model in various ways, i was thinkning maybe another way, the file could be converted into a dba file (using the rename command) and then use it in the main program, but i'm not entirely sure on how to do...

would i use the "#include" command?

maybe something like this:


or is there a better way?
Ginga
20
Years of Service
User Offline
Joined: 3rd Jul 2006
Location: Dorset, England
Posted: 23rd Sep 2006 14:35 Edited at: 23rd Sep 2006 14:37
OK. Try it this way then. I've just edited my code a wee bit, This will be based on the assumption your txt file wont read 'speed = 10' it will just read '10' (trust me, ALOT easier to figure out in code, tho its a GOOD idea to write another txt file to remind you how you formatted it, i'll give example under code), (i'll write new stuff in CAPS)



ok, 2 more examples of your text files you should have (at least the way i see it)

Text File 1 - Variable values (gamestats.txt)
should read:


Now your second text file, (format.txt) has nothing to do with the game, and once the game is completed you could delete it, as its only a referance to you.



Follow this and you should be fine. I use it for all my programs.

EDIT : somerthing wacky is happening with my code statements :S
Bluestar4
20
Years of Service
User Offline
Joined: 19th Dec 2005
Location: USA
Posted: 14th Oct 2006 23:09
back to the original quesiton, you could make an array of strings and use the load/save array commands.

example:



alternativly , you could also make a label called settings and use the data command to make a set of default values and then read these values back into the local variables (see manual)

Login to post a reply

Server time is: 2026-07-06 10:36:06
Your offset time is: 2026-07-06 10:36:06