Read from file





QuickBasic


' DOS 2+ - READ - READ FROM FILE OR DEVICE

'$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
offset = 0

inregs.BX = fileHandle
inregs.CX = 0
inregs.DX = offset
inregs.AX = &H4200

CALL InterruptX(&H21, inregs, outregs)

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

DIM buffer AS STRING  ' Buffer that will contain the bytes to read
DIM numbytes AS INTEGER  ' Number of bytes to read

buffer = SPACE$(3)
numbytes = LEN(buffer)

inregs.BX = fileHandle  ' File Handle from the opened file
inregs.CX = numbytes
inregs.DS = VARSEG(buffer)
inregs.DX = SADD(buffer)
inregs.AX = &H3F00

' DOS interrupt with sub-function &H3F

CALL InterruptX(&H21, inregs, outregs)

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

' number of bytes actually read
PRINT "Number of bytes read:"; outregs.AX