Reading and Writing Sequential Data on the Commodore 1541

Commodore LogoSometimes it’s helpful to store large amounts of data on the disk rather than keep in it memory. Consider adventure games for example: you could add room descriptions to your listing and later read it into memory – but this would double the amount of memory you really need to keep the data. Since there’s only 64KB available on a C64 that’s not smart – given that you have another 170KB sitting there next to the breadbox.

The 1541 supported reading and writing “sequential data”. It means that rather than print text and other data onto the screen, you can divert it to the floppy drive. Likewise, we could read back that data – similar to how we communicated with the 1541 when issuing CBM DOS commands.

This time we’ll

  • open a data channel (OPEN 8,8,8,”filename,direction,type”)
  • use a command (i.e. PRINT# or INPUT# or GET#)
  • close the channel

We need to specify the Read or Write direction in the process with the file name, and we also need to specify the file type. There were four to choose from: PRG, SEQ, USR and REL – corresponding to the cryptic abbreviations in the directory listing next to the file name. It was more cosmetic really and didn’t make a difference to what was happening in the actual file. Think of it as today’s file extensions.

Here’s a quick listing to illustrate the process of reading and writing sequential data:

10 print"creating a seq file on disk..."
20 open 8,8,8,"@0:sequential,w,s"
30 print#8,"this is the first line"
40 print#8,"this is the second line"
50 print#8,"and this is the last line"
51 print#8 ,47
60 close 8
100 print:print"and now lets read it back in..."
110 open 8,8,8,"sequential,r,s"
120 input#8,a$,b$,c$,d$
130 print a$
140 print b$
150 print c$
151 print"and a number: ";d$
160 print
170 close 8

Here we create a new file called “SEQUENTIAL” with the type of SEQ and add three lines of text and an integer to it. Each time we write an entry a CR character is added to the file (carriage return) which makes it easy to read those variables back in later. If the file already exists we’ll overwrite it.

Next we’ll read all values back by opening another channel (in fact the same one). The INPUT# takes in 4 strings, even though in line 51 we’ve written an integer – however behind the scenes it gets quietly converted to a string and is saved as such. Hence, if you’d like to work with numbers you must convert them back using VAL(string).

GET# works just like its BASIC counterpart GET and will read a single character from disk. This can give you more flexibility and hunt for your own “separation characters” while reading data.

You can leave a comment on my original post.