Let's continue our lesson from last time, of which my last explanation was on the commands to be able to "work" with files. Today I will talk about how to create sequential files, but before that I haven't said 2 words about the MAXFILES= command. this command makes us work with multiple files at the same time. When we turn on our MSX, the above is by default 1, so we cannot open more than one file at a time, for example if we are working in a graphic way and we have opened the GRP peripheral to write on it, we cannot open any other file. By changing this parameter, we solved the problem. The command will initialize all the variables, so it is best to always use it at the beginning of the program: MAXFILES=3. Now we can work with 3 files at the same time. It will reserve memory space for each file that is added, so it is best to use it only when needed. To create a data file we use the OPEN command and since it is a new file we must create it for writing: OPEN “[Drive:]File Name” FOR OUTPUT AS #[port number]. Unit, as I said in the previous lesson, can be the recorder and in this case it will be CAS: or DRIVE A: or B:. The rules are the same as what I said, again in the previous lesson, 6 characters without extension for cassettes and 8 characters + extension (even if it is not mandatory) for Drives. Do you remember that program I wrote a few lessons ago, an agenda, here, we'll use that for our examples. I had put the name, the address is the telephone number. After creating them we can save them on our file, which we will call “AGENDA.DAT” on disk. For the recorder it will be: CAS:AGENDA. The program line will be: OPEN “AGENDA.DAT” FOR OUTPUT AS #1. If we have more than one drive and want to create our data disk on the other, just add the drive name before the file name: “B:AGENDA.DAT”, everything we do on it will be transferred to drive B. We save our data with the PRINT #1 command. We will create the record divided into 3 fields. Now pay attention to what I will say next. If we want to record 3 fields in the same record, using PRINT #1 N$,I$,T$, which would be the Name, Address and Telephone variables, putting commas as I did, will see them as whitespace marks but not as variable dividers, and when they are read, they will be seen as the only variable. To make the MSX understand that there are more variables, we must insert them with the corresponding ASCII code or in quotes: PRINT #1, N$; “,”; I$; “,”; T$. Every time we insert a new record it will always be queued behind the last one, this will happen until we close the file with the CLOSE command: CLOSE #1 . Used without the pound sign and port number it will close all open files. On cassette, once closed the sequential file can no longer be modified, because if we reopen it in output it will delete all its contents, the same thing also applies to disk. But on the latter there is the possibility of being able to open it without deleting its contents. OPEN “AGENDA.DAT” FOR APPEND AS #1. By doing this we will continue to add our data to the file. We can also save data formatted using PRINT #1, USING “Format String”; variable, variable, . . We can also use a string variable instead of the File Name. For example, a program that works with many files and the name will be specified within it: INPUT “File Name”; NF$: OPEN NF$ FOR OUTPUT AS#1. Let's move on to the data reading phase. Now the file will be opened in input, i.e. in reading mode: OPEN”AGENDA.DAT” FOR INPUT AS #1. To read the previously written data we will use the INPUT #1 or LINE INPUT #1 command. The difference between the two is that the first will read one field at a time while the second will read the entire record even if we have added the separating commas. To give an example, with the data saving done above, if we want to read one field at a time to insert it into 3 variables, we will use: INPUT #1, N$,I$,T$, if instead we want to read the entire record, we will use: LINE INPUT #1 , A$. The variable A$ will be N$+I$+T$. When the MSX reads data from the sequential file, it will need to know when it will end, which is why there is the EOF (Port Number) function. EOF (End Of File) checks if there is still data to read. The program line to always insert before reading the file is composed as follows: IF EOF(1) THEN CLOSE #1. We will avoid an unfortunate error that the MSX will give us if we read a file where there is no more data. As mentioned above, below I insert the program with all the modifications to work with the file. It is valid only for floppy disks, but with some modifications it can also be used for cassette recorders.
5 KEY OFF: SCREEN 0,,,2: WIDTH 40: CLEAR(2000):DEFINT AZ: DIM M$(50,2)
10 ON ERROR GOTO 1000: CN=0: GOSUB 600
20 CLS:LOCATE 15.1: PRINT “MENU”: GOSUB 100
30 S$=INKEY$: IF S$=”” THEN 30
40 IF S$<“1″ OR S$>”4” THEN 30
50 ON VAL(S$) GOSUB 200,300,400,500
60 GOTO 30
100 LOCATE 1.4: PRINT”1 – DATA ENTRY”
110 LOCATE 1,8 : PRINT”2 – MODIFY DATA”
120 LOCATE 1,12: PRINT”3 – VIEW DATA”
130 LOCATE 1,20: PRINT”4 – PROGRAM OUTPUT”
140 RETURN
200 IF FRE(“”)<50 OR CN=50 THEN PRINT “ATTENTION !! No more insertions can be made”: FOR R=0 TO 2000:NEXT R: RETURN ELSE OPEN “AGENDA.DAT” FOR APPEND AS#1 205 CLS: LOCATE 12,0: PRINT “INSERT” 210 LOCATE 1,4: LOCATE 1 ,4:INPUT “NAME (MAX 10 CHARACTERS)”;N$ 215 IF LEN(N$)=0 OR LEN(N$)>10 THEN LOCATE 0,4:PRINT SPACE$(39): GOTO 210
220 LOCATE 1.8: INPUT “ADDRESS (MAX 18 CHARACTERS)”;I$
225 IF LEN(I$)=0 OR LEN(I$)>18 THEN LOCATE 0,8:PRINT SPACE$(39): GOTO 220
230 LOCATE 1,12:INPUT “PHONE (MAX 10 CHARACTERS)”; T$
235 IF LEN(T$)=0 OR LEN(T$)>10 THEN LOCATE 0,12:PRINT SPACE$(39): GOTO 230
240 LOCATE 1,18: PRINT “CORRECT (Y/N)? “
250 S$=INKEY$: IF S$=”” THEN 250
260 IF S$=”s” OR S$=”S” THEN PRINT #1,N$;”,”;I$;”,”;T$:CN=CN+1:M$(CN,0)=N$:M$(CN, 1)=I$:M$(CN,2)=T$:GOTO 280
270 IF S$=”N” OR S$=”N” THEN 205 ELSE 250
280 LOCATE 1,18: PRINT “ANOTHER ENTRY (Y/N)?”
285 S$=INKEY$: IF S$=”” THEN 285
290 IF S$=”N” OR S$=”N” THEN CLOSE #1: RETURN 20
295 IF S$=”s” OR S$=”S” THEN 205 ELSE 285
300 CLS:LOCATE 15.0: PRINT “EDIT”
303 INPUT “INSERT THE NAME TO SEARCH”;NO$
306 FOR I=1 TO CN:IF NO$=M$(I,0) THEN 310
308 NEXT: PRINT “Name not found”: FOR R=0 TO 2000:NEXT: RETURN 20
310 LOCATE 0,4:INPUT “NAME(Confirm to not change)”;N$
320 IF N$=”” THEN LOCATE 0,5:PRINT M$(I,0) ELSE M$(I,0)=N$
330 LOCATE 0.8: INPUT “ADDRESS (Confirm not to change)”;I$
340 IF I$=”” THEN LOCATE 0,9:PRINT M$(I,1) ELSE M$(I,1)=I$
350 LOCATE 0.12:INPUT “TELEPHONE (Confirm to not change)”;T$
360 IF T$=”” THEN LOCATE 0,13:PRINT M$(I,2) ELSE M$(I,2)=T$
370 OPEN “AGENDA.DAT” FOR OUTPUT AS #1
380 FOR I=1 TO CN: PRINT #1, M$(I,0);”,”;M$(I,1);”,”;M$(I,2):NEXT I
390 CLOSE #1: RETURN 20
400 CLS:LOCATE 10.0: PRINT “DISPLAY”
410 FOR I=1 TO CN
420 PRINT M$(I,0);TAB(11);M$(I,1);TAB(30);M$(I,2)
430 IF IMOD10=0 THEN GOSUB 450
440 NEXT: GOSUB 450: RETURN 20
450 PRINT:PRINT “Press space to continue”
460 IF INKEY$=” ” THEN RETURN ELSE 460
500 CLS:END
600 OPEN “AGENDA.DAT” FOR INPUT AS #1
610 IF EOF(1) THEN CLOSE #1: RETURN
620 CN=CN+1:INPUT #1,M$(CN,0),M$(CN,1),M$(CN,2)
630 GOTO 610
1000 IF ERR=53 THEN OPEN “AGENDA.DAT” FOR OUTPUT AS #1: CLOSE #1: RESUME 20
1010 RESUME NEXT
I will explain the program in the next episode.
Thank you all and see you again.


0 Comments