1 / 29

Writing and Reading Files

Writing and Reading Files. std@kmitnb.ac.th. 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.

keely
Download Presentation

Writing and Reading Files

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Writing and Reading Files std@kmitnb.ac.th

  2. 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

  3. 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

  4. Operations using File Handles • Use other services • INT 25H : Absolute read • INT 26H : Absolute write

  5. 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

  6. 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

  7. 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

  8. File Handles • Standard devices • 00 = input • 01 = output • 02 = error output • 03 = auxiliary device • 04 = printer

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. File Pointers • ตัวชี้ที่ทำหน้าที่ชี้ตำแหน่งข้อมูลของไฟล์ • เมื่อเปิดไฟล์ Pointer จะชี้ที่ไบท์แรกเสมอ • การเคลื่อนย้ายข้อมูลในไฟล์ต้องย้าย file pointer ไปยังตำแหน่งที่ต้องการ โดยใช้ INT 21H function 42H

  15. 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

  16. INT 21H Function 3CH : Create File • AH = 3CH • CX = File attribute • DX = Address of the ASCII string

  17. 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

  18. 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

  19. 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

  20. INT 21H Function 40H : Write Record • 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

  21. INT 21H Function 3EH : Close File • AH = 3EH • BX = File handle

  22. INT 21H Function 3EH : Close File • Example • MOV AH,3EH • MOV BX,FILHAND1 • INT 21H

  23. 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

  24. 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

  25. INT 21H Function 3DH : Open File • Example • FILHAND2 DW ? • . . . • MOV AH,3DH • MOV AL,00 • LEA DX,PATHNAM1 • INT 21H • JC error4 • MOV FILHAND2,AX

  26. INT 21H Function 3FH : Read Record • AH = 3FH • BX = File handle • CX = Number of bytes to read • DS:DX = Address of the input area

  27. INT 21H Function 3FH : Read Record • 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

  28. 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

  29. INT 21H Function 42H : Move File Pointer • Example • MOV AH,42H • MOV AL,00 • MOV BX,HANDLE1 • MOV CX,00 • MOV DX,1024 • INT 21H • JC error

More Related