Page 1 of 1

How to Seek in a Sequential File

Posted: Tue Oct 25, 2022 3:36 am
by TomXP411

The first section just creates a 255 byte sequential file that we can use for testing.

Starting at line 200, the program opens a sequential file named "TEST.TXT"

210 opens the command channel (secondary address 15. I use 15 as the channel number, just to keep things consistent.)

220 just shows the read pointer is at the beginning of the file

240 actually seeks to index 20 in the file (since 0 is the first byte, this will read "21")

270-280: Remember to close your channels! 


100 OPEN 1,8,2,"@:TEST.TXT,S,W"

110 FOR I=1 TO 255

120 PRINT#1,CHR$(I);

130 NEXT

140 CLOSE 1




200 OPEN 1,8,2,"TEST.TXT,S,R"

210 OPEN 15,8,15

220 GET#1,A$

230 PRINT ASC(A$)

240 PRINT#15, "P"+CHR$(2)+CHR$(20)+CHR$(0)+CHR$(0)+CHR$(0);

250 GET#1,A$

260 PRINT ASC(A$)

270 CLOSE 1

280 CLOSE 15




 


Re: How to Seek in a Sequential File

Posted: Tue Feb 28, 2023 9:50 pm
by rje
This is excellent. So the method is:

240 PRINT#15, "P"+CHR$(2)+CHR$(byte)+CHR$(block)+CHR$(offset16)+CHR$(offset24);

Where
  • "byte" is the byte offset.
    "block" is a page or block offset (256 bytes).
    "offset16" is the block of blocks offset (64k bytes).
    "offset24" is the block of 4k blocks offset (16m bytes).
Does this mean a program could potentially seek up to byte number 4,294,967,296?

Re: How to Seek in a Sequential File

Posted: Wed Mar 01, 2023 7:02 am
by TomXP411
rje wrote: Tue Feb 28, 2023 9:50 pm Does this mean a program could potentially seek up to byte number 4,294,967,296?
It's an unsigned 32-bit number. So yes, assuming the file system permits it.

Re: How to Seek in a Sequential File

Posted: Wed Mar 01, 2023 2:48 pm
by rje
I am not positive, but I don't remember fseek() working in cc65. I'll have to revisit that.

Re: How to Seek in a Sequential File

Posted: Thu Mar 02, 2023 5:14 pm
by TomXP411
rje wrote: Wed Mar 01, 2023 2:48 pm I am not positive, but I don't remember fseek() working in cc65. I'll have to revisit that.
The problem Matt had was due to his seek function trying to treat the data like string data, where it actually needs to be treated as a byte array. (Strings are null terminated, so the function saw the last couple of zero bytes as a terminator, rather than part of the data.)

What I can't remember is if we solved that here or on the Discord; you might search through here for posts by SlithyMatt regarding file seeking and C.