Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with file I/O

Status
Not open for further replies.

Elfman32

Programmer
Feb 7, 2007
1
IE
HI all.

what im trying to do is create a file (lets call is CFile) then open and read another file (RFile) and write the data to the CFile here's what i have so far :

.model small
.stack 100h

.data


;//*******************************************************************************//
;//*************************Buffers / arrays ************************************//
BUFFER DW 10 DUP (?) ;Buffer - data from the read file will be saved here
;Before being written to teh created file
FHDL DW ? ;A file handle
offsetbuffer DW 10 DUP (?) ; ????????????????
FHandle DW ?
Rhandle dw ?
Chandle dw ?
;//******************************************************************************//

;//**************************Error messages***********************************//
FNF DB 13, 10,'File Not Found', 13, 10,'$'
TMF DB 13, 10,'Too Many Files open', 13, 10,'$'
AD DB 13, 10,'Access Denied', 13, 10,'$'
IA DB 13, 10,'Invalid Access', 13, 10,'$'
RE DB 13, 10,'Read Error', 13, 10,'$'
EOF DB 13, 10,'End of File', 13, 10,'$'
;//***************************************************************************//

;//***************************File paths******************************//
CFile db 'c:\Cmprssd.txt', 0 ;File to be created
RFile DB 'sample.txt',0 ; Filename in quotes, 0 = eof char
;//*******************************************************************//


.code

BeginHere:
mov ax, @data ;set data segment
mov ds, ax

;Create and open CFile

mov ah, 3ch
mov cx, 0
mov dx, offset CFile
int 21h

jc error
mov Chandle, ax

error:
nop

;//***********************open file for reading ***************//
mov ah, 03dh ;Open file dos function
mov al, 0 ;Sets up file as read only
mov dx, offset RFile ;Filename (above) to DX
int 21h ;interupt for dos function

jc ErrorHandler ; Go to errorhandler
mov Rhandle, ax

;//*************************************************************//




;//***********************open file for writing ***************//
mov ah, 03dh ;Open file dos function
mov al, 2 ;Sets up file as read only
mov dx, offset CFile ;Filename (above) to DX
int 21h ;interupt for dos function

jc ErrorHandler ; Go to errorhandler
mov Chandle, ax

;//*************************************************************//


;//*************************************************************//
;//******** loop to read from RFile adn write to CFile ********//


LP: ; Start a segment of code to be looped
; In this case looped reading of data
; Into a buffer from a file


mov ah, 03fh ; read data
mov dx, offset buffer
mov cx, 6 ; Set number of char to be read
mov bx, Rhandle
int 21h ; Dos interupt to allow dos function to run

jc ReadError

cmp ax, cx
jne Eofile
mov si, 0


Prt_LP :

;//Write to file//
mov bx, Chandle
mov dx, buffer[si]
mov ah, 40h

mov cx, 5
int 21h
;//*************//

inc si
cmp ax, cx
jne Eofile
mov si, 0

;loop Prt_LP ;go back to next read
;jmp LP


ErrorHandler:
cmp ax, 2
jz NotFound
cmp ax, 4
jz TooMany
cmp ax, 5
jz Denied
cmp ax, 12
jz Invalid
jmp ExitProg


NotFound:
mov dx, offset FNF
mov ah, 09h
int 21h
jmp ExitProg
TooMany:
mov dx, offset TMF
mov ah, 09h
int 21h
jmp ExitProg
Denied:
mov dx, offset AD
mov ah, 09h
int 21h
jmp ExitProg
Invalid:
mov dx, offset IA
mov ah, 09h
int 21h
jmp ExitProg




Eofile:
mov cx, ax
jcxz Eof2
mov si, 0

Finish:
mov dx, buffer[si]
mov ah, 02h
int 21h
inc si
loop Finish


Eof2:
mov bx, FHDL
mov ah, 03ch
int 21h
jc Close_error

ReadError:
mov dx, offset RE
mov ah, 09h
int 21h
jmp ExitProg

Close_error:
;Do something here
;To deal with possible
;Closing errors
jmp ExitProg;


ExitProg:
mov ah, 04ch
int 21h
End BeginHere


any idea why it's not working.

It wont even create the file thanks in advance.
 
At a quick glance I can't see any files being closed, which is not an optional extra if you want stuff flushed to disk.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top