DarkBASIC Programming/File Control & Data

Day 12
Day 12 already, you're progressing towards a great career in programming DarkBASIC and programming in general! Lots of people have trouble writing files in DarkBASIC, and some have problems with Data types and more. This lesson will take a lot of time to cover, I'm going to say 5 days for you and about 2 weeks for me to write them. Hopefully, I'll get them done before but if not well you just have to wait for me or another to contribute. Today, we'll learn how to save a file.

Take for example this scenario

text$ = "Hey guess what open me up in notepad and see what happens!" set dir "C:/" if file exist("openmeupfast.txt") = 1 delete file "openmeupfast.txt" endif open to write 1,"openmeupfast.txt" write string 1,text$ close file 1 open to read 1,"openmeupfast.txt" read string 1,newtext$ Print newtext$ close file 1 wait key end

What a mouth full! In machine language it takes several thousand lines to do this same exact thing crazy or what! As you found out, the code above creates a string variable, moves the directory we'll be working in to c drive, if the file exist deletes it(or else we cannot overwrite it as it already exists), writes the file, closes it, opens it to read the contents, uses a new string to contain the line of text, then Prints it and finally you have to press any key before it ends. Now that's great! But remember the extension? You can change it to create your own files. You can also write various things such as: bytes, characters, and so on. To see more of what you can write view the command reference under the DarkBASIC help file. We'll just be using string for now and later byte.

This example will generate a syntax error and I'll tell you why now

a = 1 delete file "wrong.txt" open to write 1,"wrong.txt" write string 1,a

Remember numbers are not strings, to make a number a string we can do the following:

a = 1 a$ = str$(a) b = val(a$)

The command str$ changes a number into a value, and the command val changes a string into a value. There are many other odd things you can do with strings as well which you can see under the DarkBASIC help file command reference of strings. See some examples and change them around a bit.

Here's a useless kinda piece of software for strings

`Crazy Strings `By Dbtutor

cls message$ = "Hello World!" Print "ASC returns the ASCII number of the first letter in the str" Print asc(message$) Print "Len counts how many characters in a string" Print len(message$) Print "Mid gets the middle write characters of a string" midval = 3 Print mid$(message$,midval) Print "Here's the binary of the number 678" Print bin$(678) Print "Here's the hex of that number" Print hex$(678) Print "There are many more ways of destroying strings" Print "Much are useless, but take a gander at them" wait key end

As you saw above manuiplating strings is easy but pretty much useless. In some situations they are useful like a file manager tool which we'll be making very soon along with some other file stuff. In the 2d tutorial's final part we'll use files to make a very simple tool very much like ZZT or MegaZeux. There's lots to cover so bear with me! On day 13 we'll be learning more ways of writing files, remember byte? Well you can use it to write a value to a file. I used txt has my extension, try using .dat, .ini, etc. You can even make one up! Using write, you can also write word or an integer value, a long or long integer value, and even a float. Why I don't know.

Good Example of Writing A File:

a = 1 b$ = str$(a) if file exist("a") = 1 then delete file "a" open to write "a",1 write string 1,b$ close file 1 if file exist("a") = 1 then readit = 1 if readit = 1 open to read 1,"a" read string 1,c$ d = val(c$) Print d  Print "Same thing! Use val and str! Their better!" wait key end endif

Note: from Cody Oebel

the above code fails to work properly, and will not compile and run.

I found the If file exist("a") = 1 ... the = 1 is ambiguous, and is not needed.

below is my version of this code snippet

`Edit by Cody C Oebel: g = 11 set dir "c:/" b$ = str$(g) if file exist("a.txt") then delete file "a.txt" open to write 1, "a.txt" write string 1, b$ close file 1 if file exist ("a.txt") open to read 1, "a.txt" read string 1, z$ d = val(z$) print d print "This should work now, and is smaller amount of code" wait key endif end

Now, see what I mean? Later we'll be using current directories and file branching statements to make a simple file browser. Very simple! Write byte is sometimes used for example in tilemappers and so on but the standard is write string because it's simple. Remember keep it simple stupid!

Let's write an array dim a$(10) a$(1) = "jsdakjlakljldsaf" a$(2) = "ashjajasjhsjks"

save array a$(10),"array.ini"

load array a$(10),"array.ini"

for a = 1 to 10 Print a$(a) next a

wait key end NOTE: Again this code above did not work: By Cody Oebel

Here is my working version for other readers to see the minor mistakes.

Also note at the bottom of the compiler it shows you the format

for save array, and load array e.g. Filename, array.

I also added the variable stringname filename$ to contain "array.ini"

dim a$(10) filename$ = "array.ini" a$(1) = "jsdakjlakljldsaf" a$(2) = "ashjajasjhsjks" set dir "c:/" save array filename$, a$(10)

load array filename$, a$(10)

for a = 1 to 10 Print a$(a) next a

wait key end

Now we can save big ole arrays, database programming anyone? Remember our guessing game? Go back and fix it up a bit so that you can include this newly learned code in it. You've learned a lot about files so far. Perpare for directories on Day 13!

Day 13
Day 13 arrived and we're only two days away from the half way point. That's great news, as soon as you can be a master programmer in DarkBASIC the better! Well, really DarkBASIC takes awhile to master, not to scare you but I'm not a master programmer either. If you know somebody with a good game it's usually not that they're the best programmer but just a great game maker. Some DarkBASIC users have gone on to make commercial games such as: Dumbo & Cool, Star Wraith, etc. DarkBASIC is great for games and team efforts. Not so for applications as you'll find out today.

Here's a simple file manager

Input "Set directory to: ";direct$ set dir direct$

You don't get to see files do you? We can easily view files and whatever using some simple commands. Remember some are tedious, but when in use with loops! Now this is a file manager, you change dirs, view files, and whatnot, it's not a professional tool, it's showing you the ropes. But before we do it's time to have a look at a better file manager.

`Simple File Manager `By Dbtutor

`Get the directory to use setdirect$ = get dir$

`Set the directory to the current directory set dir setdirect$

`Current amount of items items = 10

find first Print get file name$(1)

for a = 2 to items find next Print get file name$(a) next a

Wait key end

Wow, that's cool and all but there's no user input and no file manipulation dus this program is file system viewer. Just like windows explorer! Really nowadays writing a file manager is pretty much useless especially if it's ugly. But file managers in your program in DarkBASIC allows better productivity then in typing in the file name or directory and whatnot. We're not going back to the DOS days God forbid type in a command and hope it works! Remember I said tedious? Well there is a much better way to write the following program above. Don't worry we'll be writing a file manager later! Before I show you a more productive way.

Follow these instructions:

Go to http://www.computechtenerife.com/ Register as a user. Click on Dark Basic Support and then Downloads Scroll down and find the function libraries Click on the Db file selector Download and extract the files for usage later

Okay now that's done we can see a better way of doing things in the file system viewer world. We're only a few blocks away from the file manager.

`Simple File Viewer `By Dbtutor

set dir "C:\"

Perform checklist for files for a = 1 to checklist quantity Print checklist string$(a) next a

Wait key end

See how many lines less we have? There is other perform checklist for things such as directories and devices. Go have a look and change perform checklist for files command with a few of them.

Now remember that file manager library you downloaded, if you haven't guessed we'll be modding some of the code inside the include file(not the function library) to make our own modded file manager. Remember the library is copyright to a DarkBASIC user known as TDK_MAN one of the best DarkBASIC programmers out there besides you(just flattering) I'm sure you could beat him at his game after 30 years. Just kidding if your reading this TDK no offense. He's made lots of DarkBASIC programs including MatEdit which we'll be using later on in the 3d chapters along with a 3d moddler and whatnot.

Here's the Source Code

`Thanks TDK For the Library Off your Site! Rem Fileselector Function Demo - Copyright TDK_Man May 2003 Rem Modded Version By Dbtutor gosub filemanager filemanager:
 * 1) Include "FileSelect.dba"

Set Display Mode 800,600,16 Cls Sync On

Rem Required DIM Statements At The Beginning Of Your Program Dim Files$(1000,2): Dim Drv$(26): Dim Temp$(1000,2): Dim GotDrives(1) Dim Path$(1): Dim Filename$(1): Dim SliderHeight(1): Dim FileOffset(1)

Title$="Load File": Rem Text to appear at top of fileselector Filter$="*.*": Rem 3 char filter extension (Eg: 'TXT') or *.* for all files CurDir$="C:\Program Files\Dark Basic Software": Rem Starting Directory

Rem Function Call Selected=Fileselect(CurDir$,Title$,Filter$) Rem On return Selected=1 if OK clicked and 0 if Cancel clicked

CLS If Selected=1: Rem OK Was Clicked Print "Selected Filename: ";Filename$(0) Print "Selected Path: ";Path$(0) `Modded Code here Print "Press Any Key to Manage File" wait key myfile$ = filename$(0) mydir$ = path$(0) gosub managefile Else: Rem Cancel Was Clicked Print "Cancel Button Chosen" Endif

`All Original Code Below `By Dbtutor managefile: set dir mydir$ getext$ = left$(myfile$,4) if getext$ = ".bmp" or ".jpg" then gosub view_bmp if getext$ = ".mid" or ".wav" then gosub view_audio

view_bmp: Load image myfile$,1 paste image 1,1,1 set cursor 1,400 wait key cls gosub filemanager

view_audio: load music myfile$,1 play music 1 wait key cls gosub filemanager

There you are a easy file manager thanks to other peoples function libraries! You can find lots on TDK's site(http://www.matedit.com/db.htm). Along with tutorials for functions and more. That's all for Day 13, there was no quiz for awhile so expect a lot of quizzes later on. I couldn't find anything worth quizzing you on. There will be lotsa data going on in Day 14.

Day 14
We already know what variables are, but you haven't used data statements yet. Data statements work very easily, you just put data, space and then put all the types of data.

Examples of Data

data "strings",12,5.6,"and integers and floats tooooooooooooooooo!!!!!!!" data 12 data "string me" data 5.50

You can have lots of data types in one statement as long as you separate them with commas. That's useful but how can we read the data? Well we can use read.

`Read Example data 4 read var1 Print var1

You can use restore to reset the data pointer you'll see what I'm talking about later. It's good practice to use different labels to store different data.

`Big Data Example gosub datastored

datastored: data 1234 data 12 data 1 data 4 data 56 gosub readdata

readdata: read var1 read var2 read var3 read var4 read var5

print var1 Print var2 Print var3 print var4 Print var5

First it stores data then reads it, you can do this backwards if you want. Whichever way you read and store data it works. Now we're into games, no explaining(well some) just pure games programming. We'll program a game a day woohoo let's go now!