Jump to content
  • 0

C64 Basic 2 OPEN a file as random and input fields


snakes
 Share

Question

Hi i have a couple of basic/qb45 program i want to bring to the C64. they use Open statement for access files as an example:

340 OPEN "R", *1, "TRACO", 20
350 FIELD *1, 4 AS NS$, 4 AS NI$, 4 AS LO$, 4 AS D1$, 4 AS MA$

how can i do the same or similar in C64 basic?

 

thank you very much.

 

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0
On 4/22/2021 at 9:09 PM, snakes said:

how can i do the same or similar in C64 basic?

This reference will have all you need for the C64. 

http://www.1000bit.it/support/manuali/commodore/c64/Commodore_64_Data_Files_A_Basic_Tutorial.pdf

 Once you get an understanding of the basic framework -- all disk operations on C64 are one computer ( the C64) talking to another computer, (the disk drive --which has its own 6502 processor and operating system) -- you should consult Chapter 10 of the attached for REL files (which are random access).    Warning:  Most of the emulators in my experience don't support or are very buggy in doing "REL" files.    Plus, since there's the slow I/O, the performance is not great. 

Unless your BASIC program is so big that there isn't room, I always suggest considering one of these alternatives to trying to do REL files:

a) put the information from the REL files in DATA statements as alphanumeric hex data, e.g., "DATA F1, C0, 11, A3,"   Create a random access routine to get your information from DATA statements.      You set a Variable to an INDEX to the position in the DATA statements you want, then you execute a RESTORE, followed by a for/next loop that does throw-away reads to that the number of reads equals your index minus 1.    After exiting the loop, do your next READ.      As you can see this is wasteful as hell and performance degrades in a linear way the later in the data set you need to go.    One way to deal with this is to add a quick machine code 'wedge' (see Commodore Transactor journal issues, they had these) that lets you 'RESTORE' to a particular line.   But if you are smart in how you structure things and the data list is short enough, the actual time from fetch to use can often beat the slow 1541 disk accesses. 

b)   Better still, format the data you want to have random access to into a binary file and have the first two bytes of that file set to a 16 bit address (be aware of the low/hi byte ordering) that is equal to the length of the data set subtracted from the 'normal' top of basic address, or if your data is less than 4K you can stick it in the sweet 4K of ram at $c000 to $cfff on a C64 which is always a safe place.   If you can't use the C000-cfff area, then your BASIC program a) uses a POKE to move down the top of basic by the size of the random access data; b) loads the binary file to the memory block freed up by doing so; and then whichever approach c) you create a subroutine such that you set your index into the data into a variable and then your subroutine returns a result variable that is equal to Peek (B+I) where B is the base/start address of the memory block and I is the index pointer to the data you want.    If your data items are longer than a byte, you set up the routine so you pass it a starting index offset and have that offset in the data file contain the number of bytes to read.    This takes some work to do right but its fast. 

Both of the above are better than messing with trying to do random access files on a C64

Edited by Snickers11001001
  • Thanks 1
Link to comment
Share on other sites

  • 0
  • Super Administrators
1 hour ago, desertfish said:

Seems like you will have to use the methods to read SEQuential files... open them with ,s,r and unfortunately you’ll have to read them character by character and reconstruct the fields yourself 

https://www.lemon64.com/forum/viewtopic.php?p=181963&sid=b81070e1421529e6e7b2a28a55222530#181963

This is incorrect. The 64 has a random access file mode known as RELative files. It's all in the VIC-1541 manual. 

You'll want to look at a later revision, since the earlier ones have a lot of typos...

https://archive.org/details/Commodore_1541_Disk_Drive_Users_Guide_1982-09_Commodore

 

  • Thanks 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

Please review our Terms of Use