Writing to Files

There are many ways to write to files, but the easiest that I've found is the one I'll discuss. It involves OPENing the file for OUTPUT (from your program), then WRITING the information to the file. It can be used to save information (such as games, variables, the speed of someones computer or other setup information, etc) for later use. Heck, I've even used it to create a .bat file for running one of my programs. To open the file, use the following code:

open "c:\your\file\name\filename.ext" for output as #1

All that did was open the file, so now you have to write to it. You can only write variables with this technique, so just say yourvariable$ = "Hello" or something to input text. You can also use this for other types of variables. Here's an example:

write #1, yourvariable

That was pretty easy, so now let's try to write a code to input two variable (this can be used for an unlimited number of variables). All you do is separate the variables with a comma.

write #1, yourvariable, yourvariable2

Sample File:
8, 10

That's assuming that yourvariable = 8 and yv2 = 10.

Okay, lets say you want to write two variables that won't be separated by a comma in the save file. To do that, you put a plus sign between the variables instead of a comma. Here's an example:

write #1, yourvariable + yourvariable2

Sample File:
8 10

That's assuming that yourvariable = 8 and yv2 = 10.

Here's what it would look like with and without a comma between them:

With comma, and let's say that yourvariable$ = "Hello." and yourvariable2$ = "How are you?":
"Hello.", "How are you?"

Without comma, same variables:
"Hello. How are you?"

I think the difference between these is pretty obvious. That should be all you need to know about writing to files. Now go on to read the 'reading that info' tutorial immediately after this one.

Back to the Tutorials...

Email: cybertron@flashmail.com