fopen() with append
-
- Posts: 24
- Joined: Sat Jan 27, 2024 7:22 pm
fopen() with append
Just a quick question. I know fseek(...) isn't implemented, so does that mean fopen("file.dat", "ab") doesn't work either?
- ahenry3068
- Posts: 1218
- Joined: Tue Apr 04, 2023 9:57 pm
Re: fopen() with append
\russell-s-harper wrote: ↑Fri Mar 21, 2025 2:37 am Just a quick question. I know fseek(...) isn't implemented, so does that mean fopen("file.dat", "ab") doesn't work either?
I'm not doing a bunch of C work. But for any extensive disk I/O in cc65 it's probably best to use the cbm_ functions.
Commodore disk I/O doesnt' map neatly onto the standard C library so cbm_ stuff probably works faster anyway. There is a seek and append function built into the kernal though they work differently than what you might normally expect.
If your not familiar with the low level details Working with CBM DOS in the Community docs page is a good reference.
This is my file seek function in BASIC. CHANNEL is the Secondary address.
So if I use OPEN 4,8, 2, "MYFILE,S,R" then the CHANNEL argument would be 2 not 4
(This is also why I would never use OPEN 4,8,2 in my code. I always use the same number for LFN & Secondary address
for this very reason, but it is valid to use different ones. Using the same number is for MY benefit)
This is called with GOSUB SEEK.POSITION. CHANNEL & FILEPOS are the arguments to the GOSUB
and are set first. (Just GLOBAL variables in BASIC).
Code: Select all
## Can be set to any valid file device
DEVICE = 8
## CONVERT TO SIGNED RANGE
DEF FN SW(W)=W + 65536 * (W > 32767)
## CONVERT TO UNSIGNED RANGE
DEF FN UW(W)=W-65536*(W<0)
DEF FN Lo.Byte(X)=(FN SW(X)) AND $FF
DEF FN Hi.Byte(X)=INT(FN UW(X)/256)
SEEK.POSITION:
HIWORD = INT(FILEPOS/65536)
LOWORD = FILEPOS - (HIWORD*65536)
P0 = FN Lo.Byte(LOWORD)
P1 = FN Hi.Byte(LOWORD)
P2 = FN Lo.Byte(HIWORD)
P3 = FN Hi.Byte(HIWORD)
POSITION:
COMMAND$="P"+CHR$(CHANNEL)+CHR$(P0)+CHR$(P1)+CHR$(P2)+CHR$(P3)
GOTO DOS.CMD
DOS.CMD:
OPEN 15,DEVICE,15, COMMAND$
GOTO GETFCODE
GETFILECODE:
OPEN 15,DEVICE,15
GETFCODE:
INPUT#15,FCode,FC$,A,B:CLOSE 15:RETURN