Write to File or Device





QuickBasic


' DOS 2+ - WRITE - WRITE TO 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 = &H3D01

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 = 1

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

CALL InterruptX(&H21, inregs, outregs)

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

DIM buffer AS STRING  ' data to write
DIM numbytes AS INTEGER  ' number of bytes to write

buffer = "hello"
numbytes = LEN(buffer)

inregs.BX = fileHandle
inregs.CX = numbytes
inregs.DS = VARSEG(buffer)
inregs.DX = SADD(buffer)

' DOS interrupt with sub-function &H40

inregs.AX = &H4000

CALL InterruptX(&H21, inregs, outregs)

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

PRINT "Number of bytes written:";STR$(outregs.AX)