Function 40h Write File or Device

mov bx, Handle ;handle of file or device

mov cx, MaxBytes ;maximum number of bytes to write

mov dx, seg Buffer

mov ds, dx

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

mov ah, 40h ;Write File or Device

int 21h

jc error_handler ;carry set means error

mov ActualBytes, ax ;number of bytes written

Write File or Device (Function 40h) writes up to the specified number of bytes of data from a buffer to a file or device.

Parameters

Handle

Identifies the file or device that is to receive the data.

MaxBytes

Specifies the maximum number of bytes to write.

Buffer

Points to a buffer that contains the data to write.

Return Value

If the function is successful, the carry flag is clear and the AX register contains the number of bytes written to 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 writes to a file, it writes data starting at the current location of the file pointer. When this function returns, the file pointer is positioned at the byte immediately after the last byte written to the file.

Writing 0 bytes to the file truncates the file at the current position of the file pointer.

If the number of bytes written is fewer than the number requested, the destination file or disk is full. Note that the carry flag is not set in this situation.

See Also

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