Int 21H [2.0] Function 3DH (61) Open file

Given an ASCIIZ pathname, opens the specified file in the designated or default directory on the designated or default disk drive. A handle is returned which can be used by the program for subsequent access to the file.

Call with:

AH = 3DH

AL = access mode

Bit(s) Significance

0—2 access mode

000 = read access

001 = write access

010 = read/write access

3 reserved (0)

4—6 sharing mode (MS-DOS versions 3.0 and later)

000 = compatibility mode

001 = deny all

010 = deny write

011 = deny read

100 = deny none

7 inheritance flag (MS-DOS versions 3.0 and later)

0 = child process inherits handle

1 = child does not inherit handle

DS:DX = segment:offset of ASCIIZ pathname

Returns:

If function successful

Carry flag = clear

AX = handle

If function unsuccessful

Carry flag = set

AX = error code

Notes:

Any normal, system, or hidden file with a matching name will be opened by this function. If the file is read-only, the success of the operation also depends on the access code in bits 0—2 of register AL. After opening the file, the file read/write pointer is set to offset zero (the first byte of the file).

The function fails if:

any element of the pathname does not exist.

the file is opened with an access mode of read/write and the file has the read-only attribute.

[3.0+] SHARE.EXE is loaded and the file has already been opened by one or more other processes in a sharing mode that is incompatible with the current program's request.

The file's date and time stamp can be accessed after a successful open call with Int 21H Function 57H.

The file's attributes (hidden, system, read-only, or archive) can be obtained with Int 21H Function 43H.

When a file handle is inherited by a child process or is duplicated with Int 21H Function 45H or 46H, all sharing and access restrictions are also inherited.

[2] Only bits 0—2 of register AL are significant; the remaining bits should be zero for upward compatibility.

[3.0+] Bits 4—7 of register AL control access to the file by other programs. (Bits 4—6 have no effect unless SHARE.EXE is loaded.)

[3.0+] A file-sharing error causes a critical-error exception (Int 24H) with an error code of 02H. Int 21H Function 59H can be used to obtain information about the sharing violation.

[4.0+] Int 21H Function 6CH combines the services of Functions 3CH, 3DH, and 5BH.

Example:

Open the file C:\MYDIR\MYFILE.DAT for both reading and writing, and save the handle for subsequent access to the file.

fname db 'C:\MYDIR\MYFILE.DAT',0

fhandle dw ?

.

.

.

mov ah,3dh ; function number

mov al,2 ; mode = read/write

mov dx,seg fname ; address of pathname

mov ds,dx

mov dx,offset fname

int 21h ; transfer to MS-DOS

jc error ; jump if open failed

mov fhandle,ax ; save file handle

.

.

.