LESSON 24 – THE DATA MANAGED BY THE MSX (PART 3)

da | Mar 2, 2024 | MSX BASIC | 0 comments

Kind regards to everyone. Today we will close this topic, I will talk about direct or random access files. But first, as I mentioned in the last episode, I will give a small explanation about the program. First, the first thing to say
is that I used a command that I haven't talked about yet: error handling, ON ERROR GOTO. But I will, maybe in the next lesson. I did this because, at the beginning the program needs to know if the file exists, if it is not found, the MSX will report the error; File Not Found. But with error trapping we will make sure that if the file does not exist, it will be created. However, if the file has already been created, the program will continue by opening it in Append mode and load all the data into the matrix (Line 20 ... GOSUB 600), updating the CN counter. The insertion subroutine, which goes from line 200 to 295, after the necessary checks will open the file in APPEND. The data will be stored after our confirmation. We will be able to make more insertions, the file will be closed only when we exit the aforementioned routine. The modification of one or more fields (from line 300 to 390) will be done within the matrix, since everything is stored within them. Upon exit it will copy all the matrices on the disk by opening the file in OUTPUT mode, in fact it will write the new modified file, deleting the old one. On the data display (line 400 to 460), I have not made any changes, I have only placed the data in columns. You could do the sorting and then save it to disk, but I leave this task to you. However, for those who want to do it on cassette, it is very simple, remove all the openings with APPEND and work only with the matrices. When the program exits, open the file in OUTPUT and record it, the writing and reading procedures are the same.

Now let's talk about Random Files. What is the difference between the two? It's easy to say.
While on the first, the sequential, in order to read a piece of data you have to read all those that come before it, for the Radom we can go directly to that piece of data.
It is a little more complex than the first one, but it is useful for working with a lot of data and is also faster. The first thing to say, this type of file can be read and written at the same time, the big problem is that first we have to determine the length of the records not exceeding 256 characters. To open the file we will use this syntax: OPEN “File Name” AS # Port number LEN Record length. The length of the record must be the sum of the fields that form it, it only accepts alphanumeric characters, therefore all numerical values must be converted into strings. Let's start with an example by taking the previous program, which has the limit in the insertion. There are 38 characters.

600 OPEN “AGENDA.DAT” AS #1 LEN=38

610 FIELD #1, 10 AS N1$, 18 AS I1$, 10 AS T1$

Our created file has a record of 38 characters, with the FIELD function we have divided it into 3 fields. The sum of them must correspond to the data entered when creating the file. To save a record in the file we must use the PUT function # port number, Record number, port number in our case is 1, while record number is the number we want to give, we will certainly always give the lowest free number, otherwise it would replace all the data that he has inside. Before saving it we must convert it. We will use LSET if we want to write it starting from the left or RSET from the right, much better to use LSET. As you have seen, when creating the fields I used different variables, the reason is that they do not have to be the same as those used by the program.

620 LSET N1$=N$

630 LSET I1$=I$

640 LSET T1$=T$

650 PUT #1, 1

We inserted our record into the file. Being the first I put the number 1 but you can also use a variable, the NS that we use to count the records on the previous program. This is not our case but if in the program there had been a numerical variable called for example R, we had to convert it with the function MKI$ if it had been integer, MKS$ if it had been single precision or MKD$ if it had been double precision. Let's assume that our variable had been integer, to prepare it to be saved to the file, the correct syntax would have been as follows: LSET R$=MKI$(R) . To read the data we will use the GET # Port Number, Record Number function

700 GET #1, 1

710 PRINT N1$; i1$; T1$

Here too, if we need to convert one of the variables into numeric, taking the example above with the variable R$, we will use the functions CVI for integers, CVS for single precision and CVD for double precision: R=CVI(R$). The CLOSE command closes the open file or files, as I have already explained in the sequential files. Finally, if we want to know the last record read or written, we will use the LOC(port number) function, it will return the record number, it is understood that it must be used with the open file: PRINT LOC(1) or NR=LOC( 1)

Before closing this lesson I will talk about the MERGE command, I had to do it at the beginning of this topic (Part 1), but I missed it, let's solve it immediately. The above command is used to concatenate 2 program files. To do this the program must be saved with the SAVE command in ASCII mode, this is done by adding the letter A at the end: SAVE “File Name”, A, otherwise it is not feasible. MERGE will add the entire file to the one in memory, so if there are lines with the same number it will replace them. Be careful when doing this. One last important thing, if the resident file is very large and the one we add will be equally large, thus exceeding the capacity of the RAM, an error will be reported: Out of Memory.

And with that and all. Thanks and see you next time.

0 Comments

Submit a Comment

Your email address will not be published. I campi obbligatori sono contrassegnati *

LEZIONE 26 – INTERVAL ON, RND ED ALTRI

LESSON 26 – INTERVAL ON, RND AND OTHERS

Good morning everyone, In this episode I will talk about an important interrupt, especially for games, when we want to make an enemy suddenly appear or a useful object, but also to create a stopwatch or a clock and many other things. The COMMAND exploits the...

LEZIONE 26 – INTERVAL ON, RND ED ALTRI

LESSON 21 – MSX and music

The MSX has a great sound chip that can 'play' three voices at once. Let's start with the classic command that we often hear in various programs or when an error occurs. Its name is BEEP, that is, by writing the name of this command we reproduce this sound. AND'...