The Complete STOS Reference

 pre-release 0.2 Edited By Dr Phibes

Disk Access Basics

By Tony Greenwood (For STOSSER Diskzine)

 LOADING | SAVING
Disk (or is it disc) access is a very important and powerful part of programming. Don't you just hate playing a game on your ST for hours and finally making the highscore table only for it not to be saved to disk and therefore losing it forever. I would much prefer to know that the next person who is going to play will be able to marvel at my high score. Not just high score tables can be saved and retrieved from your disk, there are numerous uses and advantages that can be gained from disk accessing. The file you are now reading has been accessed from disk of course. Theres also a save to disk option in the help screen, I am sure you can think of many more uses. But do you know how to do it, I think not or you would'nt be reading this file.

Well i have the problem of not knowing how much or how little you do know, therefore i will have to start from scratch and presume you know nothing on the subject. A little boring for some of you i know, but at least i know i will have covered all levels of competence.

Well i have the STOS Manual at my side to refer to. Its now some time since i learnt how to use these commands and its only now that i realise just how useless the manual is. The commands i first learnt and the ones we will be covering ie: SEQUENTIAL FILES, "FILENAME.SEQ"

Before i go any further i will stop you falling into the same trap i did when i was trying to learn these, and that is the fact that the files we want to access do not have to have SEQ as there extensions. So we can use all the following on DOC, TXT, MUS, ASC etc etc...

Right then, the word sequence means that any access to these files will be done in order from start of file to end of file. In other words if you wanted to load line 300 from a DOC file you would have to load the first 299 lines to get to it. Random access files would let you load line 300 on its own, or save line 300 on its own. In order for us to save one line we would have to save the whole file, apart from the making of a database program i can think of no reason to want to load just the one part of a file. In fact i have never needed that system so we will just concentrate on SEQUENCIAL FILES.

LOADING

First off before we open a file and load it into memory we will need somewhere to put it. Lets concentrate on loading a whole doc file into memory shall we?. So we would load this doc file line by line, the doc file could be any legnth could'nt it, so we will DIM an array of 1000. This will then enable us to load any doc file that is 1000 lines long or less. If you think you may need bigger then simply DIM a bigger array of course, so we are off!.
10 DIM TXT$(1000)
Next we need to find a file to open, so we will use the file selector to find one. You will find the instructions for this in page 218 of your manual. But i have just looked at it and its not very clear is it, here i will show you what to do.
20 F$=FILE SELECT$ ("*.*")
This will bring up a file selector that will list all the files and folders in the root directory. The "*" simply mean show all, so it will show all the file names and all the extensions. If you only wanted DOC files to be listed then you would change it to:
20 F$=FILE SELECT$ ("*.DOC")
Then whatever file you click on, F$ will hold the name of that file, so if you click on READ_ME .DOC , then thats what F$ would equal.

Next we want to open that file, we can have more than one file open at once, so we have to keep them numbered. We then use that number for everything we do to that file so as not to confuse STOS. If you are only using one file at once then you must still number it. So to keep things as clear as possible we will call this one number one, but it must be prefixed by a #,So we are working on file #1

30 OPEN IN #1, F$
"Open" speaks for itself ( i hope ) ,"F$" holds the filename,"#1" is the number of the file we are using, "IN" means we are opening the file for retrieving/getting data and bringing it into our program. The oposite function ie: saving data is the opposite command ie: open out #1, f$, but dont worry about that yet, so long as you fully understand line 30 and what each peice means.

Now we obviously want more than one peice of information bringing in, ie: each line of the DOC file is a seperate peice of info that we will be puttting into each cell of our array. Therefore we will now need to set up a loop in order to bring it all in.

40 E=0 : REPEAT
50 LINE INPUT #1,TXT$(E)
60 INC E
70 UNTIL EOF(#1)
80 DEC E
90 CLOSE #1 : rem we have got it!
 Now to explain the above,

SAVING

Well this bit should be a lot shorter than the above as it is quite simple now we have covered the main points of disk accessing, we are quite simply going to do the opposite of the loading instructions, let me explain..

To open a file for Saving something to disk we use the following command.

OPEN OUT #1, "FILENAME.DOC"
Or if you have just loaded a doc file or an high score table or something with a variable as in the loading commands, open in #1, F$, then you can use the same variable
OPEN OUT #1,F$
Then instead of INPUT #1, TXT$(E) we would do the opposite and..
PRINT #1, TXT$(E)
So to save out the doc file you loaded in with the loading commands, you would now use the following.
100 OPEN OUT #1,F$
110 FOR N=0 TO E
120 PRINT #1,TXT$(N)
130 NEXT N
140 CLOSE #1
The EOF(#1) will not work when saving out, but we already know how many lines to save as we got that with the E variable, so its easy enough to make a controlled loop from 0 to E and we cannot miss then can we.

Please note: When you open out a filename, it will erase any file with that filename thats on the disk. Therefore if you had a doc file with lets say five hundred lines in it and you wanted to add one line, you cannot open the file and print to disk just line TXT$(501) ,because then the file with that filename will then simply consist of the one line you just saved. Hence the explanation earlier about SEQUENCE, you would have to load the whole five hundred lines, then add one line, then save the five hundred and one lines.

The use of RANDOM ACCESS files will however let you load or save just one line, but i dont know enough on random access files to enable me to write an article on the subject, If you do then write it up for us!.

Well i hope the above is enough to get you started on using the saving and loading commands that are at your disposal. There are lots of other commands that can be used in conjunction with the above. I suggest that once you can do the above then the rest will come to you a lot easier with the use of your trusty manual.

 HAPPY STOSSING!