29
Writing and Reading Files [email protected]

Writing and Reading Files [email protected]. Methods for processing disk files File Control Blocks (FCBs) –Supported by DOS –Can address drives and filenames

Embed Size (px)

Citation preview

Writing and Reading Files

[email protected]

Methods for processing disk files

• File Control Blocks (FCBs)– Supported by DOS– Can address drives and filenames– Cannot address directories

• File Handles– Accessing of the file– Return codes to identity errors

Operations using File Handles

• Use INT 21H services– 3CH : Create file– 3DH : Open file– 3EH : Close file– 3FH : Read record– 40H : Write record– 42H : Move file pointer

Operations using File Handles

• Use other services– INT 25H : Absolute read– INT 26H : Absolute write

Operations using FCBs

• Use INT 21H services– 0FH : Open file– 10H : Close file– 14H : Read record– 15H : Write record– 16H : Create file– 21H : Read record randomly– 22H : Write record randomly– 27H : Read block randomly– 28H : Write block randomly

ASCIIZ strings

• Containing the filespec :– the location of the disk drive– directory path– filename– ‘All optional within apostrophes’ – a bytes of hex zeros– maximum length string is 128 bytes

ASCIIZ strings

• Example1 : defines a drive and filename

– PATHNAM1 DB ‘D:\Filetest.asm’, 00H

• Example2 : defines a drive, subdirectory and filename

– PATHNAM2 DB ‘E:\Utility\Filetest.exe’, 00H

File Handles

• Standard devices– 00 = input– 01 = output– 02 = error output– 03 = auxiliary device– 04 = printer

Error Return Codes

• File handle operations for disk deliver a “completion status” via– Carry flag– Register AX

• Successful operation– clear carry flag & perform other functions

• Unsuccessful operation– set carry flag & return an error code in AX

Lists of error codes : 01-36

• 01 : Invalid function number• 02 : File not found• 03 : Path not found• 04 : Too many file open• 05 : access denied• 06 : Invalid handle• 07 : Memory control block destroyed• 08 : Insufficient memory• 09 : Invalid memory block address• 10 : Invalid environment

Lists of error codes : 01-36

• 11 : Invalid format• 12 : Invalid access code• 13 : Invalid data• 15 : Invalid drive specified• 16 : Attempt to remove directory• 17 : Not same device• 18 : No more files• 19 : Write-protected disk• 20 : Unknown unit

Lists of error codes : 01-36

• 21 : Invalid function number• 22 : Unknown command• 23 : CRC data error• 24 : Bad request structure length• 25 : Seek error• 26 : Unknown media type• 27 : Sector not found• 28 : Printer out of paper• 29 : Write fault• 30 : Read fault

Lists of error codes : 01-36

• 31 : General failure• 32 : Sharing violation• 33 : Lock violation• 34 : Invalid disk change• 35 : FCB unavailable• 36 : Sharing buffer overflow

File Pointers

• ตั�วชี้��ที่��ที่หน้ ที่��ชี้��ตัแหน้�งข้ อมู�ลข้องไฟล�

• เมู��อเปิ�ดไฟล� Pointer จะชี้��ที่��ไบที่�แรกเสมูอ

• กรเคล��อน้ย้ ย้ข้ อมู�ลใน้ไฟล�ตั องย้ ย้ file

pointer ไปิย้�งตัแหน้�งที่��ตั องกร โดย้ใชี้ INT

21H function 42H

Using File handles to create disk files

• Procedure for writing a disk– Use an ASCIIZ string to get a file handle

from the system– Use INT 21H function 3CH to create the file– Use INT 21H function 40H to write records

in the file– Use INT 21H function 3EH to close the file

INT 21H Function 3CH : Create File

• AH = 3CH

• CX = File attribute

• DX = Address of the ASCII string

INT 21H Function 3CH : Create File

• File attribute– 00H : Normal file– 01H : Read only– 02H : Hidden file– 04H : System file– 08H : Volume label– 10H : Subdirectory– 20H : Archive file

INT 21H Function 3CH : Create File

• Example– PATHNAM1 DB ‘E:\ACCOUNTS.FIL’, 00H– FILHAND1 DW ?– . . .– MOV AH,3CH– MOV CX,00– LEA DX,PATHNAM1– INT 21H– JC error– MOV FILHAND1,AX

INT 21H Function 40H : Write Record

• AH = 40H

• BX = Stored file handle

• CX = Number of bytes to write

• DS:DX = Address of the output area

• Example– FILHAND1 DW ?– OUTAREA DB 256 DUP(‘ ‘)– . . .– MOV AH,40H– MOV BX,FILHAND1– MOV CX,256– LEA DX,OUTAREA– INT 21H– JC error2– CMP AX,256– JNE error3

INT 21H Function 40H : Write Record

INT 21H Function 3EH : Close File

• AH = 3EH

• BX = File handle

• Example– MOV AH,3EH– MOV BX,FILHAND1– INT 21H

INT 21H Function 3EH : Close File

Using File handles to read disk files

• Procedure for reading a disk file– Use an ASCIIZ string to get a file handle from

the system– Use INT 21H function 3DH to open the file– Use INT 21H function 3FH to read records

from the file– Use INT 21H function 3EH to close the file

INT 21H Function 3DH : Open File

• AH = 3DH

• AL = Access code– 00 : Read only– 01 : Write only– 02 : Read/Write

• DX = Address of the ASCII string

• Example– FILHAND2 DW ?– . . .– MOV AH,3DH– MOV AL,00– LEA DX,PATHNAM1– INT 21H– JC error4– MOV FILHAND2,AX

INT 21H Function 3DH : Open File

INT 21H Function 3FH : Read Record

• AH = 3FH

• BX = File handle

• CX = Number of bytes to read

• DS:DX = Address of the input area

• Example– FILHAND2 DW ?– INAREA DB 512 DUP(‘ ‘)– . . .– MOV AH,3FH– MOV BX,FILHAND2– MOV CX,512– LEA DX,INAREA– INT 21H– JC error5– CMP AX,00– JE endfile

INT 21H Function 3FH : Read Record

INT 21H Function 42H : Move File Pointer

• AH = 42H

• BX = File handle

• CX : DX = Offset address required

• AL = Method code– 00 : Take the offset from the start of the file– 01 : Take the offset from the current location

of the file pointer– 02 : Take the offset from the end-of-file

• Example– MOV AH,42H– MOV AL,00– MOV BX,HANDLE1– MOV CX,00– MOV DX,1024– INT 21H– JC error

INT 21H Function 42H : Move File Pointer