Function 3Fh Read File or Device

mov bx, Handle ;handle of file or device

mov cx, MaxBytes ;maximum number of bytes to read

mov dx, seg Buffer

mov ds, dx

mov dx, offset Buffer ;ds:dx points to buffer to receive data

mov ah, 3Fh ;Read File or Device

int 21h

jc error_handler ;carry set means error

mov ActualBytes, ax ;number of bytes read

Read File or Device (Function 3Fh) reads up to the specified number of bytes of data from a file or device into a buffer. MS-DOS may read fewer than the specified number of bytes if it reaches the end of the file.

Parameters

Handle

Identifies the file or device to be read from.

MaxBytes

Specifies the maximum number of bytes to read.

Buffer

Points to the buffer that is to receive data from the file or device. The buffer must be at least as large as MaxBytes.

Return Value

If the function is successful, the carry flag is clear, Buffer contains the data read from the file or device, and the AX register contains the number of bytes read from the file or device. Otherwise, the carry flag is set and the AX register contains an error value, which may be one of the following values:

Value Name

0005h ERROR_ACCESS_DENIED
0006h ERROR_INVALID_HANDLE

Comments

Handle can be a handle for a standard device or a handle created by using such a function as Open File with Handle (Function 3Dh).

When MS-DOS reads from a file, it reads data starting at the current location of the file pointer. When this function returns, the file pointer is positioned at the byte immediately following the last byte read from the file.

This function can also be used to read from the standard input device (typically the keyboard). If MS-DOS is reading from standard input, this function returns if it reads a carriage-return character (ASCII 0Dh), even if it has not yet read the number of bytes specified in MaxBytes.

If this function returns zero for the number of bytes read, the file pointer is at the end of the file. If the number of bytes read is fewer than the number requested, MS-DOS reached the end of the file during the read operation.

See Also

Function 3Ch Create File with Handle
Function 3Dh Open File with Handle
Function 40h Write File or Device
Function 42h Move File Pointer
Function 5Ah Create Temporary File
Function 5Bh Create New File
Function 6Ch Extended Open/Create