# 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
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
Remember numbers are not strings, to make a number a string we can do the following:
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
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:
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
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
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"
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
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.
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:
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.
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
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
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.
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.
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!