Set current file position





QuickBasic


' DOS 2+ - LSEEK - SET CURRENT FILE POSITION

'$INCLUDE: 'qb.bi'

DIM file AS STRING
DIM inregs  AS RegTypeX
DIM outregs AS RegTypeX

file = "C:\INT\123.txt" + CHR$(0)
inregs.DS = VARSEG(file)
inregs.DX = SADD(file)
inregs.AX = &H3D00

CALL InterruptX(&H21, inregs, outregs)

IF outregs.FLAGS AND 1 THEN
   PRINT "Error opening >> " + file$
   BEEP: END
END IF

DIM fileHandle AS INTEGER
DIM offset AS INTEGER

fileHandle = outregs.AX  ' File Handle from the above 'Open Existing File' interrupt call
offset = 1  ' Offset from origin of new file position

inregs.BX = fileHandle
inregs.CX = 0
inregs.DX = offset

' AH - &H42
' AL - origin of move
'      00h start of file
'      01h current file position
'      02h end of file

inregs.AX = &H4200 + AL

' DOS interrupt with sub-function &H42

CALL InterruptX(&H21, inregs, outregs)
'outregs.ax = new file position = 1

IF outregs.FLAGS AND 1 THEN
   PRINT "Error setting current file position >> " + file$
   BEEP: END
END IF

DIM newFilePosition AS INTEGER
newFilePosition = outregs.AX

offset = 1
inregs.BX = fileHandle
inregs.CX = 0
inregs.DX = offset
inregs.AX = &H4200
'if inregs.AL = 0 (start of file) - outregs.ax = new file position = 1
'if inregs.AL = 1 (current file position) - outregs.ax = new file position = 2

CALL InterruptX(&H21, inregs, outregs)

IF outregs.FLAGS AND 1 THEN
   PRINT "Error setting current file position >> " + file$
   BEEP: END
END IF

PRINT "Current File Position:"; STR$(outregs.AX)