|
5鱼币
利用文件句柄将两个文件合并成一个文件。要求: 1)在第一个文件末尾处换行回车,插入“The second file as follow:”; 2)换行回车,把第二个文件内容存放在后面。这是我写的,运行不正确,求教改正
DATA SEGMENT
NUMBER DW ?
SFILE DB 40
DB ?
DB 41 DUP(?)
DFILE DB 40
DB ?
DB 41 DUP(?)
W_BUF DB 64
DB ?
W_STR DB 64 DUP(0)
ASK1 DB 0AH,0DH,'INPUT SOURCE FILE:$'
ASK2 DB 0AH,0DH,'INPUT DEST FILE:$'
ERR1 DB 0AH,0DH,'CREATE FILE ERROR:$'
ERR2 DB 0AH,0DH,'OPEN FILE ERROR:$'
ERR3 DB 0AH,0DH,'READ FILE ERROR:$'
ERR4 DB 0AH,0DH,'WRITE FILE ERROR:$'
ERR5 DB 0AH,0DH,'CLOSE FILE ERROR:$'
ERR6 DB 0AH,0DH,'DISK FULL ERROR:$'
MESS1 DB 0AH,0DH,'Please input characters$'
MESS2 DB 0AH,0DH,'Write file failed$'
BUFR DB ?
BUFFER DB 512 DUP(0)
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA,ES:DATA
MAIN PROC FAR
MOV AX,DATA
MOV DS,AX
MOV ES,AX
LEA DX,ASK1 ;提示输出文件路径
MOV AH,9
INT 21H
LEA DX,SFILE ;输入路径的字符
CALL INPUT
MOV CL,SFILE+1 ;记录字符个数
MOV CH,0
MOV SI,CX ;偏移地址
MOV SFILE[SI+2],0 ;最后一位赋值0
LEA DX,SFILE+2 ;打开文件
MOV AH,3DH
MOV AL,2
INT 21H
JNC MFH
OERR:
LEA DX,ERR2
ERR:
MOV AH,09H
INT 21H
MOV AH,4CH
INT 21H
MFH:
MOV BL,AL
MOV BUFR,AL
MOV CX,0
MOV DX,0
MOV AL,2
MOV AH,42H ;设置文件指针
INT 21H
JNC WSTR
DEC DX
WSTR:
LEA DX,MESS1 ;提示输入字符
MOV AH,09H
INT 21H
LEA DX,W_BUF ;输入内容
MOV AH,0AH
INT 21H
LEA DX,W_STR
MOV CL,BYTE PTR[W_BUF+1] ;记录字符串长度
MOV CH,0H
MOV AH,40H ;写入字符
INT 21H
JC W_ERROR
WSTR1:
LEA DX,W_STR
XCHG BL,BUFR
MOV AH,40H ;写文件
INT 21H
JNC NEXT
LEA DX,ERR4
JMP ERR
NEXT:
MOV CX,DX
MOV DX,AX
MOV AL,0
MOV AH,42H
INT 21H
LEA DX,ASK2
MOV AH,09H
INT 21H
LEA DX,DFILE ;输入目标文件
CALL INPUT
MOV CL,DFILE+1
MOV CH,0
MOV SI,CX
MOV DFILE[SI+2],0
LEA DX,DFILE+2
MOV AH,3DH ;打开文件
MOV AL,0
INT 21H
JNC RFILE
JMP OERR
RFILE: MOV BX,AX
RFILE1:
LEA DX,BUFFER ;读取文件
MOV CX,512
MOV AH,3FH
INT 21H
JNC WFILE
LEA DX,ERR3
JMP ERR
WFILE:
OR AX,AX
JZ EXIT
MOV CX,AX
LEA DX,BUFFER
XCHG BL,BUFR
MOV AH,40H ;写文件
INT 21H
JNC R_W
LEA DX,ERR4
JMP ERR
CMP AX,CX
JE R_W
LEA DX,ERR6
JMP ERR
R_W:
XCHG BL,BUFR
JMP RFILE1
EXIT:
MOV AH,3EH ;关闭文件
INT 21H
JNC EXIT1
EXIT0:
LEA DX,ERR5
JMP ERR
EXIT1:
XCHG BL,BUFR
MOV AH,3EH
INT 21H
JC EXIT0
JMP L
W_ERROR:
LEA DX,MESS2
MOV AH,09H
INT 21H
MOV AH,4CH
L: INT 21H
RET
MAIN ENDP
INPUT PROC NEAR
MOV AH,0AH
INT 21H
RET
INPUT ENDP
CODE ENDS
END MAIN
|
|