1 hour ago, rje said:
You're welcome.
And thank you review team! It's been decades since I did any file ops on Commodore equipment.
Note that there's an "input pattern" required if you're expecting to have null values in the data stream -- I can't quite remember it, but I think it involves injecting a chr$(0) into the input to safeguard against it.
So the issue with CHR$(0) and NULL is a problematic one.
Writing a zero out to disk works fine: PRINT#8, CHR$(0);
However, reading a Zero byte from disk with the GET command will actually return a Zero Length String, aka "". So, because we obviously need file I/O in BASIC to be even slower, you have to check every read against "" when reading binary data. So the pattern goes like this:
100 OPEN 8,8,8,"MyFile,S,R"
110 A=0
120 GET#8, A$
130 IF A$<>"" THEN A=ASC(A$)
140 IF ST <> 0 THEN 200
150
do things 160 GOTO 110
200
check command channel for errors 250 CLOSE 8
The critical part is lines 110-130. This starts A with a value of zero, so when we get to the IF statement, we already have the null value handled. The IF sets A to the value of the character read for non-null values, and then we get to to the core of the program at 150.
Line 140 is just the standard EOF check, which diverts to 200 to check the disk status and close the file. You really want to read the command channel in line 200 to make sure that you didn't get some sort of error on the last GET before closing the file.
I feel like this might be a good time to sit down and do some proper tutorials for disk I/O, covering basic I/O, error handling, and the Position (or seek) command.