How to Seek in a Sequential File

Tutorials and help articles.

Articles will be approved by our staff before posting. This is to ensure that the community gets the highest quality possible content.
Post Reply
TomXP411
Posts: 1726
Joined: Tue May 19, 2020 8:49 pm

How to Seek in a Sequential File

Post 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




 

rje
Posts: 1262
Joined: Mon Apr 27, 2020 10:00 pm
Location: Dallas Area

Re: How to Seek in a Sequential File

Post 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?
Last edited by rje on Wed Mar 01, 2023 2:47 pm, edited 1 time in total.
TomXP411
Posts: 1726
Joined: Tue May 19, 2020 8:49 pm

Re: How to Seek in a Sequential File

Post 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.
rje
Posts: 1262
Joined: Mon Apr 27, 2020 10:00 pm
Location: Dallas Area

Re: How to Seek in a Sequential File

Post by rje »

I am not positive, but I don't remember fseek() working in cc65. I'll have to revisit that.
TomXP411
Posts: 1726
Joined: Tue May 19, 2020 8:49 pm

Re: How to Seek in a Sequential File

Post 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.
Post Reply