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 / File Help

Author
Message
D a r k
20
Years of Service
User Offline
Joined: 21st Jun 2004
Location: In ur fridge, eatin ur foodz.
Posted: 10th Mar 2005 13:10
Ok im having trouble with some of the File Commands.

For the open to write file, how do a write stuff to an existing file ???

For the open to read file, how do I get like a certain thing from that file for example if I had a .txt file that said this:
"Health = 10
Name$ = "Bob"
"

How would I make it read the Variable Bob and save it as Name$ on my program?
Underworld 1020
21
Years of Service
User Offline
Joined: 2nd Mar 2004
Location: NY, USA
Posted: 10th Mar 2005 22:47 Edited at: 17th Mar 2005 05:35
Take a look here:
http://forum.thegamecreators.com/?m=forum_view&t=47667&b=10

The second method is much simpler, but the first method can be used and I actually perfer it .

Here's a break down of the first method:

First you need to check if the file you want to create already exists or not. If it does already exist then you need to delete it because the "Open To Write" command creates only a new file.

The code will look like this:


Then you need to actually create the file.
Like This:


Then you need to store your data in the new file. There are a few different ways you can do this by using either the "Write Byte" command or the "Write String" command. I usually use the "Write Byte" command because then the user can't open the file and change the data and stuff, well they could, but the won't no what it means .

So Either Like This:

Or Like This:


Then after you have stored all the data you want to the file, you have to close the file.
Like This:

That's it for storing all or data, now all you got to do is retrieve all the data from the file .

So, the first step would be opening the file by using the "Open To Read" command. However, make sure the file exists or the command will fail and cause an error.
So Like This:


Then you have to read in all the data from the file to your program.
Either Like This:

Or Like This:


However, if you saved the data to the file as a byte then you must use the "Read Byte" command and its the same case with a string.

Then you must close the file.
Like This:


And that's all there is to it, although it may look like a lot of work its not really when you get used to it, eventually you'll be able to do it in like 20 seconds.

The final code would look like this:


D a r k
20
Years of Service
User Offline
Joined: 21st Jun 2004
Location: In ur fridge, eatin ur foodz.
Posted: 13th Mar 2005 08:16
Ok that helps, but what if I want to load two things from one file?

Like if i have Player_Health and Player_Lives ont he same .txt file how do I load them both? Cause when I try to load them both for some reason they both always = Player_Health

For example

Player_Health = 10 and Player_Lives = 5 so I write both of those to a file but when I try to load the data Playerhealth and Player_Live both = 10. How can I fix this?
Underworld 1020
21
Years of Service
User Offline
Joined: 2nd Mar 2004
Location: NY, USA
Posted: 15th Mar 2005 22:00
Well, if you saved "Player_Health" first, then you have to load "Player_Health" first before you can load "Player_Lives". You just have to load the data in the same order as you saved it.

So Your Code Would Look Like This:

First Save The Data:


Then You Load The Data:


Just make sure you load the data in the same order that you saved it in. Let me know if you have anymore questions .

Login to post a reply

Server time is: 2025-05-30 17:08:35
Your offset time is: 2025-05-30 17:08:35