2022-11-23 00:14:43 +00:00
|
|
|
; this program shows the use of the f_seek function to seek to a position in an opened file.
|
|
|
|
; (this only works on Commander X16 DOS. on sdcard, not on host filesystem.)
|
|
|
|
|
|
|
|
%import diskio
|
|
|
|
%import textio
|
|
|
|
%zeropage basicsafe
|
|
|
|
%option no_sysinit
|
|
|
|
|
|
|
|
main {
|
2022-11-23 22:20:24 +00:00
|
|
|
uword megabuffer = memory("megabuffer", 20000, 256)
|
2022-11-23 00:14:43 +00:00
|
|
|
|
|
|
|
sub start() {
|
2022-11-23 22:20:24 +00:00
|
|
|
|
2023-05-01 23:48:56 +00:00
|
|
|
txt.print("writing data file (drive 8)...\n")
|
2022-11-23 00:14:43 +00:00
|
|
|
uword total=0
|
2023-05-01 23:48:56 +00:00
|
|
|
if diskio.f_open_w("@:seektestfile.bin") {
|
2022-11-23 22:20:24 +00:00
|
|
|
repeat 100 {
|
2023-05-01 23:48:56 +00:00
|
|
|
str text = "hello world.*"
|
2024-11-23 14:51:38 +00:00
|
|
|
void diskio.f_write(text, strings.length(text))
|
|
|
|
total += strings.length(text)
|
2022-11-23 00:14:43 +00:00
|
|
|
}
|
|
|
|
diskio.f_close_w()
|
|
|
|
txt.print("written size=")
|
|
|
|
txt.print_uw(total)
|
|
|
|
txt.nl()
|
|
|
|
} else {
|
|
|
|
txt.print("error: ")
|
2023-05-01 23:48:56 +00:00
|
|
|
txt.print(diskio.status())
|
2022-11-23 00:14:43 +00:00
|
|
|
sys.exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
read_last_bytes()
|
|
|
|
|
2023-11-11 13:26:40 +00:00
|
|
|
txt.print("\nseeking to 1292 and writing a few bytes...\n")
|
2023-11-12 19:40:17 +00:00
|
|
|
if diskio.f_open_w_seek("seektestfile.bin") {
|
2023-11-11 13:26:40 +00:00
|
|
|
diskio.f_seek_w(0, 1292)
|
|
|
|
void diskio.f_write("123", 3)
|
|
|
|
diskio.f_close_w()
|
|
|
|
} else {
|
|
|
|
txt.print("error: ")
|
|
|
|
txt.print(diskio.status())
|
|
|
|
sys.exit(1)
|
|
|
|
}
|
2023-05-02 17:21:01 +00:00
|
|
|
|
|
|
|
read_last_bytes()
|
2022-11-23 00:14:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub read_last_bytes() {
|
2022-11-23 22:20:24 +00:00
|
|
|
; read the last 10 bytes of the 1300 bytes file
|
2022-11-23 00:14:43 +00:00
|
|
|
uword total = 0
|
|
|
|
uword size
|
2022-11-23 22:20:24 +00:00
|
|
|
txt.print("\nreading...\n")
|
2023-05-01 23:48:56 +00:00
|
|
|
if diskio.f_open("seektestfile.bin") {
|
2022-11-23 22:20:24 +00:00
|
|
|
size = diskio.f_read_all(megabuffer)
|
|
|
|
diskio.f_close()
|
|
|
|
txt.print("size read:")
|
|
|
|
txt.print_uw(size)
|
|
|
|
txt.nl()
|
|
|
|
} else {
|
|
|
|
txt.print("error!\n")
|
|
|
|
sys.exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
txt.print("\nseeking to 1290 and reading...\n")
|
2023-05-01 23:48:56 +00:00
|
|
|
if diskio.f_open("seektestfile.bin") {
|
2023-05-02 01:11:09 +00:00
|
|
|
diskio.f_seek(0, 1290)
|
2022-11-23 22:20:24 +00:00
|
|
|
uword ptr = megabuffer
|
2022-11-23 00:14:43 +00:00
|
|
|
do {
|
2022-11-23 22:20:24 +00:00
|
|
|
size = diskio.f_read(ptr, 255)
|
2022-11-23 00:14:43 +00:00
|
|
|
total += size
|
2022-11-23 22:20:24 +00:00
|
|
|
ptr += size
|
2022-11-23 00:14:43 +00:00
|
|
|
} until size==0
|
|
|
|
diskio.f_close()
|
|
|
|
txt.print("size read=")
|
|
|
|
txt.print_uw(total)
|
|
|
|
txt.nl()
|
2022-11-23 22:20:24 +00:00
|
|
|
megabuffer[lsb(total)] = 0
|
2022-11-23 00:14:43 +00:00
|
|
|
txt.print("buffer read=")
|
|
|
|
ubyte idx
|
|
|
|
for idx in 0 to lsb(total-1) {
|
2022-11-23 22:20:24 +00:00
|
|
|
txt.print_ubhex(megabuffer[idx], false)
|
2022-11-23 00:14:43 +00:00
|
|
|
txt.spc()
|
|
|
|
}
|
2023-05-01 23:48:56 +00:00
|
|
|
txt.print("\nas text: \"")
|
2022-11-23 22:20:24 +00:00
|
|
|
txt.print(megabuffer)
|
2023-05-01 23:48:56 +00:00
|
|
|
txt.print("\"\n")
|
2022-11-23 00:14:43 +00:00
|
|
|
} else {
|
|
|
|
txt.print("error: ")
|
2023-05-01 23:48:56 +00:00
|
|
|
txt.print(diskio.status())
|
2022-11-23 00:14:43 +00:00
|
|
|
sys.exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|