[Coco] RANDOM ACCESS FILES IN DISK BASIC

Allen Huffman alsplace at pobox.com
Mon Jan 23 22:50:09 EST 2023


> On Jan 23, 2023, at 7:21 PM, coco--- via Coco <coco at maltedmedia.com> wrote:
> 
> Hi Trying to figure out how to open a random access file in disk basic.
> 
> Tried this code
> 
> 10 DIR 0
> 11 PRINT "FILE NAME"
> 12 INPUT F$
> 14 F$=F$+":0"
> 16 PRINT F$
> 20 OPEN "D",X,F$,1



Here is a quick example that makes a file, then gets records (1 byte entry):

0 'DIRECT.BAS
10 '
11 ' CREATE A FILE
12 '
20 OPEN "O",#1,"FILE.TXT"
30 PRINT #1,"DON'T PANIC!"
40 CLOSE #1
100 '
101 ' OPEN DIRECT ACCESS
102 '
110 OPEN "D",#1,"FILE.TXT",1
115 FIELD #1,1 AS BT$
120 NR=LOF(1)
130 PRINT "RECORDS: ";NR
140 FOR R=1 TO NR
150 GET #1,R
170 PRINT ASC(BT$);
180 NEXT
190 CLOSE #1


For your example:

Line 20, the X would be set to 0, and device #0 would be the keyboard.

20 OPEN “D”,#1,F$,1

…that should fix that. It will open F$ as a series of one-byte entries. But, doing an INPUT#1 or LINE INPUT#1 may fail since they want to read to a carriage return.

Using FIELD will let you specify what the records are. In my example, 1 byte will be BT$.

You can do something like

OPEN “D”,#1,”FILE.DAT”,10

…and each GET will be a 10 byte record. After the open you can define what is in each record like:

FIELD #1,3 AS A$,2 AS B$, 5 AS C$

Now when you “GET #1,3” to retrieve record 3 you won’t need a LINE INPUT after it. It will automatically load the bytes in to the variables specified by FIELD. Kinda cool.

Hope this helps.

--
Allen Huffman - PO Box 7634 - Urbandale IA 50323 - 515-999-0227 (vmail/TXT only)
http://www.subethasoftware.com - https://www.facebook.com/subethasoftware




More information about the Coco mailing list