Int 21H [2.0] Function 42H (66) Set file pointer

Sets the file location pointer relative to the start of file, end of file, or current file position.

Call with:

AH = 42H

AL = method code

00H absolute offset from start of file

01H signed offset from current file pointer

02H signed offset from end of file

BX = handle

CX = most significant half of offset

DX = least significant half of offset

Returns:

If function successful

Carry flag = clear

DX = most significant half of resulting file pointer

AX = least significant half of resulting file pointer

If function unsuccessful

Carry flag = set

AX = error code

Notes:

This function uses a method code and a double-precision (32-bit) value to set the file pointer. The next record read or written in the file will begin at the new file pointer location. No matter what method is used in the call to this function, the file pointer returned in DX:AX is always the resulting absolute byte offset from the start of file.

Method 02H may be used to find the size of the file by calling Int 21H Function 42H with an offset of 0 and examining the pointer location that is returned.

Using methods 01H or 02H, it is possible to set the file pointer to a location that is before the start of file. If this is done, no error is returned by this function, but an error will be encountered upon a subsequent attempt to read or write the file.

Examples:

Using the file handle from a previous open or create operation, set the current file pointer position to 1024 bytes after the start of file.

fhandle dw ?

.

.

.

mov ah,42h ; function number

mov al,0 ; method = absolute

mov bx,fhandle ; file handle

mov cx,0 ; upper half of offset

mov dx,1024 ; lower half of offset

int 21h ; transfer to MS-DOS

jc error ; jump, function failed

.

.

.

The following subroutine accepts a record number, record size, and handle and sets the file pointer appropriately.

; call this routine with BX = handle

; AX = record number

; CX = record size

; returns all registers unchanged

;

setptr proc near

push ax ; save record number

push cx ; save record size

push dx ; save whatever's in DX

mul cx ; size * record number

mov cx,ax ; upper part to CX

xchg cx,dx ; lower part to DX

mov ax,4200h ; function number & method

int 21h ; transfer to MS-DOS

pop dx ; restore previous DX

pop cx ; restore record size

pop ax ; restore record number

ret ; back to caller

setptr endp