Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

How to read data from a file??

[b][red]This message was edited by Polish Pimp at 2003-6-12 12:1:49[/red][/b][hr]
in this file romero.txt should be read, and should dispaly the line after line along with a line number preceding each line, i cant get the data to show


.MODEL SMALL
.CODE
.STARTUP

CALL OPEN_F ;open file for read
CALL READ_F ;read file to buffer
MOV AH, 02



MOV AH,09 ;copy buffer to screen
MOV DX,OFFSET BUFFER_2
INT 21H
CALL CLOSE_F ;close file
.EXIT

READ_F PROC NEAR
MOV AH,3FH ;read from file
MOV BX,HANDLE_F1 ;use handle
MOV CX,500 ;number of bytes to read
MOV DX,OFFSET BUFFER_2 ;DX points to buffer
INT 21H ;invoke interrupt
JNC R_EXIT
MOV DX,OFFSET ERR_READ ;display error message
CALL ERR_MSG
R_EXIT: RET
READ_F ENDP

CLOSE_F PROC NEAR
MOV AH,3EH ;close file function
MOV BX,HANDLE_F1 ;use handle
INT 21H ;invoke interrupt
JNC CL_EXIT
MOV DX,OFFSET ERR_CLOSE ;display error message
CALL ERR_MSG

CL_EXIT: RET
CLOSE_F ENDP

OPEN_F PROC NEAR
MOV AH,3DH ;open file function
MOV AL,0 ;read only
MOV DX,OFFSET FILE_1 ;DX points to ASCIIZ
INT 21H ;invoke interrupt
JC O_ERR
MOV HANDLE_F1,AX ;save handle if OK
JMP O_EXIT

O_ERR: MOV DX,OFFSET ERR_OPEN ;display error message
CALL ERR_MSG

O_EXIT: RET
OPEN_F ENDP

ERR_MSG PROC NEAR ;DX points to msg before call
MOV AH,09H ;output to monitor
INT 21H ;invoke interrupt
RET

ERR_MSG ENDP

.DATA
HANDLE_F1 DW 0 ;file1 handle
FILE_1 DB 'C:Romeo.txt',0 ;file1 ASCIIZ string
BUFFER_2 DB 400 DUP (' '),'$'
ERR_OPEN DB 0DH,0AH,'** Error opening file **$'
ERR_READ DB 0DH,0AH,'** Error reading file **$'
ERR_CLOSE DB 0DH,0AH,'** Error closing file **$'
EXIT_MSG DB 0DH,0AH,'PRESS ANY KEY TO EXIT$'
;
.STACK 32
END
Sign In or Register to comment.