Page 1 of 1
Open file for reading and other file for writing at the same time
Posted: Tue Nov 07, 2023 9:04 pm
by unartic
I want to make a custom file copy function. See code below.
If the second OPEN and the line with JSR CHKOUT are commented out, the JSR CHRIN will result in loading the first byte of the inputfile into A.
But, when the second OPEN is executed, JSR CHRIN does not read a byte from the inputfile.
I can't figure out why.
;Open file for INPUT
lda #5
ldx #8
ldy #2
jsr SETLFS
lda #(end_filename-filename)
ldx #<filename
ldy #>filename
jsr SETNAM
jsr OPEN
LDX #5
jsr CHKIN
;Open file for OUTPUT
lda #6
ldx #8
ldy #6
jsr SETLFS
lda #(end_filenamedest-filenamedest)
ldx #<filenamedest
ldy #>filenamedest
jsr SETNAM
jsr OPEN
LDX #1
JSR CHKOUT
JSR CHRIN
Re: Open file for reading and other file for writing at the same time
Posted: Tue Nov 07, 2023 9:38 pm
by DragWx
You're correctly telling the Kernal that you want to open something for input or output, but you also need to tell the storage device whether the thing you're opening is supposed to be for reading or for writing.
To tell the drive itself you want to open a file for writing, you need to add ",S,W" to the end of the filename, so for example: "OUTFILE,S,W". The ",S" part doesn't mean anything on the SD card, so don't worry about what it means; the ",W" part is what specifies that you want to write to the file.
The next fun thing is, files are not allowed to be overwritten by default, so to specify that you'd like to overwrite a file if it already exists is to add "@:" to the beginning of the filename, so like this: "@:OUTFILE,S,W".
Re: Open file for reading and other file for writing at the same time
Posted: Wed Nov 08, 2023 7:15 am
by unartic
Thanks for your answer. However, I do not have problems writing to a file. The problem is reading.
I did some further testing:
If I first open the inputfile and then open the output file, reading the inputfile doesn't work, writing does work.
If I first open the outputfile and then open the inputfile, writing to the outputfile doesn't work, reading input files does work.
So it seems that only the last opened file is working like expected, but I read it should be possible to have up to 10 files open at the same time.
Am I missing something here?
![Wink ;-)](./images/smilies/icon_e_wink.gif)
Re: Open file for reading and other file for writing at the same time
Posted: Wed Nov 08, 2023 8:04 am
by ahenry3068
;Open file for OUTPUT
lda #6
ldx #8
ldy #6
jsr SETLFS
lda #(end_filenamedest-filenamedest)
ldx #<filenamedest
ldy #>filenamedest
jsr SETNAM
jsr OPEN
LDX #1 (SHOULD THIS BE LDX #6)
JSR CHKOUT
(and maybe LDX #5 again here ?)
JSR CHRIN
I'm just trying to learn here myself. But the way I was reading it that might be the problem.
If I'm wrong I certainly want to know. I'm trying to look at small snippets of ASM like this and
see how well I'm getting them before I try to do my own ASM code.
Re: Open file for reading and other file for writing at the same time
Posted: Wed Nov 08, 2023 8:19 am
by unartic
You are right in this code snippet
![Smile :)](./images/smilies/icon_e_smile.gif)
But tested already with the correct logical filenumbers
Re: Open file for reading and other file for writing at the same time
Posted: Wed Nov 08, 2023 8:22 am
by unartic
Just to clarify: I’ve tested with loading the correct logical file into X just before CHRIN and CHROUT, but that does also not work unfortunately.
Re: Open file for reading and other file for writing at the same time
Posted: Wed Nov 08, 2023 4:50 pm
by DragWx
Ah, sorry about that. I thought the problem was that two files were being opened as "read", which is why you weren't getting input from the first file after opening the second file. That's probably not what's wrong.
I did some more digging, and the problem could be that IO devices can only either be an input or an output, not both at the same time. If device 8 had your input file, and device 9 had your output file, then things would work the way we're assuming: CHRIN does the expected read, and CHROUT does the expected write.
However, since both files are on the same device, I think you need to switch back and forth between which file is currently "selected". To do this, try modifying your loop to do CHKIN, CHRIN, CHKOUT, CHROUT. This should flip device 8 back and forth between input and output on the Kernal side, and should flip between the two currently-open files on the disk side.
Re: Open file for reading and other file for writing at the same time
Posted: Thu Nov 09, 2023 2:46 pm
by unartic
Thanks! With your help I figured it out. I indeed need to call CHKIN and CHKOUT each time I want to read or write. And I messed up the accumulator being populated by calling READST.