如何从一个文件复制到另一个文件?
How to copy from one file to another?
我正在尝试从一个文件复制到另一个文件。因此,如果提示用户输入文件名...FILE1,则将保存的字符串复制到文件中,然后提示用户输入第二个文件名...File2,将文件 1 的内容复制到文件 2 .我可以找到有关读取部分的信息,但我不确定如何从 1 复制到另一个???
.model small
.stack 100h
.data
handle dw ?
filename db 26 ;MAX NUMBER OF CHARACTERS ALLOWED (25).
db ? ;LENGTH (NUMBER OF CHARACTERS ENTERED BY USER).
db 26 dup(0) ;CHARACTERS ENTERED BY USER. END WITH CHR(13).
prompt1 db "ENTER FILE NAME HERE: $"
mess1 db ' I WIN! $'
.code
main:
mov ax, @data ; set up addressability of data
mov ds, ax
;DISPLAY MESSAGE.
lea dx, prompt1 ; load and print the string PROMPT
mov ah, 9
int 21h
;CAPTURE FILENAME FROM KEYBOARD.
mov ah, 0Ah
mov dx, offset filename ;THIS VARIABLE REQUIRES THE 3-DB FORMAT.
int 21h
;CAPTURED STRING ENDS WITH CHR(13), BUT TO CREATE FILE WE NEED
;THE FILENAME TO END WITH CHR(0), SO LET'S CHANGE IT.
mov si, offset filename + 1 ;NUMBER OF CHARACTERS ENTERED.
mov cl, [ si ] ;MOVE LENGTH TO CL.
mov ch, 0 ;CLEAR CH TO USE CX.
inc cx ;TO REACH CHR(13).
add si, cx ;NOW SI POINTS TO CHR(13).
mov al, 0
mov [ si ], al ;REPLACE CHR(13) BY 0.
;CREATE FILE.
mov ah, 3ch ; dos service to create file
mov cx, 0
mov dx, offset filename + 2 ;CHARACTERS START AT BYTE 2.
int 21h
jc failed ; end program if failed
mov handle, ax ; save file handle
mov DI, 100 ;CAN'T USE CX BECAUSE WE NEED IT TO WRITE TO FILE.
PL:
;WRITE STRING ON FILE.
mov ah, 40h ; write to
mov bx, handle ; file
mov dx, offset mess1 ; where to find data to write
mov cx, 7 ;LENGTH OF STRING IN CX.
int 21h
DEC DI ;DECREASE COUNTER.
jnz PL
mov ah, 3Eh ; close file
mov bx, handle ; which file
int 21h
failed:
mov ah, 4ch
int 21h
end main
好的,杰克,我有 运行 你的代码,而且效果很好。您需要的是复制粘贴一些已经存在的部分,然后添加三个:打开第一个文件、创建另一个文件和传输数据。我修改了您的代码以实现目标,就在这里,将其复制粘贴到您的编译器中,如果它有效,现在就告诉我:
.model small
.stack 100h
.data
handle dw ?
handle2 dw ?
filename db 26 ;MAX NUMBER OF CHARACTERS ALLOWED (25).
db ? ;LENGTH (NUMBER OF CHARACTERS ENTERED BY USER).
db 26 dup(0) ;CHARACTERS ENTERED BY USER. END WITH CHR(13).
filename2 db 26 ;MAX NUMBER OF CHARACTERS ALLOWED (25).
db ? ;LENGTH (NUMBER OF CHARACTERS ENTERED BY USER).
db 26 dup(0) ;CHARACTERS ENTERED BY USER. END WITH CHR(13).
prompt1 db 13,10,"ENTER FILE NAME HERE: $"
prompt2 db 13,10,"ENTER A SECOND FILE NAME: $"
mess1 db ' I WIN! $'
buf db ?
.code
main:
mov ax, @data ; set up addressability of data
mov ds, ax
;DISPLAY MESSAGE.
lea dx, prompt1 ; load and print the string PROMPT
mov ah, 9
int 21h
;CAPTURE FILENAME FROM KEYBOARD.
mov ah, 0Ah
mov dx, offset filename ;THIS VARIABLE REQUIRES THE 3-DB FORMAT.
int 21h
;CAPTURED STRING ENDS WITH CHR(13), BUT TO CREATE FILE WE NEED
;THE FILENAME TO END WITH CHR(0), SO LET'S CHANGE IT.
mov si, offset filename + 1 ;NUMBER OF CHARACTERS ENTERED.
mov cl, [ si ] ;MOVE LENGTH TO CL.
mov ch, 0 ;CLEAR CH TO USE CX.
inc cx ;TO REACH CHR(13).
add si, cx ;NOW SI POINTS TO CHR(13).
mov al, 0
mov [ si ], al ;REPLACE CHR(13) BY 0.
;CREATE FILE.
mov ah, 3ch ; dos service to create file
mov cx, 0 ;READ/WRITE MODE.
mov dx, offset filename + 2 ;CHARACTERS START AT BYTE 2.
int 21h
jc failed ; end program if failed
mov handle, ax ; save file handle
mov DI, 100 ;CAN'T USE CX BECAUSE WE NEED IT TO WRITE TO FILE.
PL:
;WRITE STRING ON FILE.
mov ah, 40h ; write to
mov bx, handle ; file
mov dx, offset mess1 ; where to find data to write
mov cx, 7 ;LENGTH OF STRING IN CX.
int 21h
DEC DI ;DECREASE COUNTER.
jnz PL
;CLOSE FILE.
mov ah, 3Eh ; close file
mov bx, handle ; which file
int 21h
;OPEN FILE TO READ FROM IT.
mov ah, 3DH
mov al, 0 ;READ MODE.
mov dx, offset filename + 2
int 21h
mov handle, ax ; save file handle
;DISPLAY MESSAGE FOR SECOND FILE.
lea dx, prompt2 ; load and print the string PROMPT
mov ah, 9
int 21h
;CAPTURE FILENAME FROM KEYBOARD.
mov ah, 0Ah
mov dx, offset filename2 ;THIS VARIABLE REQUIRES THE 3-DB FORMAT.
int 21h
;CAPTURED STRING ENDS WITH CHR(13), BUT TO CREATE FILE WE NEED
;THE FILENAME TO END WITH CHR(0), SO LET'S CHANGE IT.
mov si, offset filename2 + 1 ;NUMBER OF CHARACTERS ENTERED.
mov cl, [ si ] ;MOVE LENGTH TO CL.
mov ch, 0 ;CLEAR CH TO USE CX.
inc cx ;TO REACH CHR(13).
add si, cx ;NOW SI POINTS TO CHR(13).
mov al, 0
mov [ si ], al ;REPLACE CHR(13) BY 0.
;CREATE FILE.
mov ah, 3ch ; dos service to create file
mov cx, 0 ;READ/WRITE MODE.
mov dx, offset filename2 + 2 ;CHARACTERS START AT BYTE 2.
int 21h
jc failed ; end program if failed
mov handle2, ax ; save file handle
;READ ALL BYTES FROM FIRST FILE AND WRITE THEM TO SECOND FILE.
reading:
;READ ONE BYTE.
mov ah, 3FH
mov bx, handle
mov cx, 1 ;HOW MANY BYTES TO READ.
mov dx, offset buf ;THE BYTE WILL BE STORED HERE.
int 21h ;NUMBER OF BYTES READ RETURNS IN AX.
;CHECK EOF (END OF FILE).
cmp ax, 0 ;IF AX == 0 THEN EOF.
je eof
;WRITE BYTE TO THE SECOND FILE.
mov ah, 40h ; write to
mov bx, handle2 ; file
mov dx, offset buf ; where to find data to write
mov cx, 1 ;LENGTH OF STRING IN CX.
int 21h
jmp reading ;REPEAT PROCESS.
eof:
;CLOSE FILES.
mov ah, 3Eh ; close file
mov bx, handle ; which file
int 21h
mov ah, 3Eh ; close file
mov bx, handle2 ; which file
int 21h
failed:
mov ah, 4ch
int 21h
end main
顺便说一句:一次读取一个字节效率不高,实际上,如果文件不是太大,可以一次读取所有文件(效率很高)。另一种选择是两个两个或十个字节读取字节,只处理最后一次读取,因为可能会读取不到 10 个字节。
我正在尝试从一个文件复制到另一个文件。因此,如果提示用户输入文件名...FILE1,则将保存的字符串复制到文件中,然后提示用户输入第二个文件名...File2,将文件 1 的内容复制到文件 2 .我可以找到有关读取部分的信息,但我不确定如何从 1 复制到另一个???
.model small
.stack 100h
.data
handle dw ?
filename db 26 ;MAX NUMBER OF CHARACTERS ALLOWED (25).
db ? ;LENGTH (NUMBER OF CHARACTERS ENTERED BY USER).
db 26 dup(0) ;CHARACTERS ENTERED BY USER. END WITH CHR(13).
prompt1 db "ENTER FILE NAME HERE: $"
mess1 db ' I WIN! $'
.code
main:
mov ax, @data ; set up addressability of data
mov ds, ax
;DISPLAY MESSAGE.
lea dx, prompt1 ; load and print the string PROMPT
mov ah, 9
int 21h
;CAPTURE FILENAME FROM KEYBOARD.
mov ah, 0Ah
mov dx, offset filename ;THIS VARIABLE REQUIRES THE 3-DB FORMAT.
int 21h
;CAPTURED STRING ENDS WITH CHR(13), BUT TO CREATE FILE WE NEED
;THE FILENAME TO END WITH CHR(0), SO LET'S CHANGE IT.
mov si, offset filename + 1 ;NUMBER OF CHARACTERS ENTERED.
mov cl, [ si ] ;MOVE LENGTH TO CL.
mov ch, 0 ;CLEAR CH TO USE CX.
inc cx ;TO REACH CHR(13).
add si, cx ;NOW SI POINTS TO CHR(13).
mov al, 0
mov [ si ], al ;REPLACE CHR(13) BY 0.
;CREATE FILE.
mov ah, 3ch ; dos service to create file
mov cx, 0
mov dx, offset filename + 2 ;CHARACTERS START AT BYTE 2.
int 21h
jc failed ; end program if failed
mov handle, ax ; save file handle
mov DI, 100 ;CAN'T USE CX BECAUSE WE NEED IT TO WRITE TO FILE.
PL:
;WRITE STRING ON FILE.
mov ah, 40h ; write to
mov bx, handle ; file
mov dx, offset mess1 ; where to find data to write
mov cx, 7 ;LENGTH OF STRING IN CX.
int 21h
DEC DI ;DECREASE COUNTER.
jnz PL
mov ah, 3Eh ; close file
mov bx, handle ; which file
int 21h
failed:
mov ah, 4ch
int 21h
end main
好的,杰克,我有 运行 你的代码,而且效果很好。您需要的是复制粘贴一些已经存在的部分,然后添加三个:打开第一个文件、创建另一个文件和传输数据。我修改了您的代码以实现目标,就在这里,将其复制粘贴到您的编译器中,如果它有效,现在就告诉我:
.model small
.stack 100h
.data
handle dw ?
handle2 dw ?
filename db 26 ;MAX NUMBER OF CHARACTERS ALLOWED (25).
db ? ;LENGTH (NUMBER OF CHARACTERS ENTERED BY USER).
db 26 dup(0) ;CHARACTERS ENTERED BY USER. END WITH CHR(13).
filename2 db 26 ;MAX NUMBER OF CHARACTERS ALLOWED (25).
db ? ;LENGTH (NUMBER OF CHARACTERS ENTERED BY USER).
db 26 dup(0) ;CHARACTERS ENTERED BY USER. END WITH CHR(13).
prompt1 db 13,10,"ENTER FILE NAME HERE: $"
prompt2 db 13,10,"ENTER A SECOND FILE NAME: $"
mess1 db ' I WIN! $'
buf db ?
.code
main:
mov ax, @data ; set up addressability of data
mov ds, ax
;DISPLAY MESSAGE.
lea dx, prompt1 ; load and print the string PROMPT
mov ah, 9
int 21h
;CAPTURE FILENAME FROM KEYBOARD.
mov ah, 0Ah
mov dx, offset filename ;THIS VARIABLE REQUIRES THE 3-DB FORMAT.
int 21h
;CAPTURED STRING ENDS WITH CHR(13), BUT TO CREATE FILE WE NEED
;THE FILENAME TO END WITH CHR(0), SO LET'S CHANGE IT.
mov si, offset filename + 1 ;NUMBER OF CHARACTERS ENTERED.
mov cl, [ si ] ;MOVE LENGTH TO CL.
mov ch, 0 ;CLEAR CH TO USE CX.
inc cx ;TO REACH CHR(13).
add si, cx ;NOW SI POINTS TO CHR(13).
mov al, 0
mov [ si ], al ;REPLACE CHR(13) BY 0.
;CREATE FILE.
mov ah, 3ch ; dos service to create file
mov cx, 0 ;READ/WRITE MODE.
mov dx, offset filename + 2 ;CHARACTERS START AT BYTE 2.
int 21h
jc failed ; end program if failed
mov handle, ax ; save file handle
mov DI, 100 ;CAN'T USE CX BECAUSE WE NEED IT TO WRITE TO FILE.
PL:
;WRITE STRING ON FILE.
mov ah, 40h ; write to
mov bx, handle ; file
mov dx, offset mess1 ; where to find data to write
mov cx, 7 ;LENGTH OF STRING IN CX.
int 21h
DEC DI ;DECREASE COUNTER.
jnz PL
;CLOSE FILE.
mov ah, 3Eh ; close file
mov bx, handle ; which file
int 21h
;OPEN FILE TO READ FROM IT.
mov ah, 3DH
mov al, 0 ;READ MODE.
mov dx, offset filename + 2
int 21h
mov handle, ax ; save file handle
;DISPLAY MESSAGE FOR SECOND FILE.
lea dx, prompt2 ; load and print the string PROMPT
mov ah, 9
int 21h
;CAPTURE FILENAME FROM KEYBOARD.
mov ah, 0Ah
mov dx, offset filename2 ;THIS VARIABLE REQUIRES THE 3-DB FORMAT.
int 21h
;CAPTURED STRING ENDS WITH CHR(13), BUT TO CREATE FILE WE NEED
;THE FILENAME TO END WITH CHR(0), SO LET'S CHANGE IT.
mov si, offset filename2 + 1 ;NUMBER OF CHARACTERS ENTERED.
mov cl, [ si ] ;MOVE LENGTH TO CL.
mov ch, 0 ;CLEAR CH TO USE CX.
inc cx ;TO REACH CHR(13).
add si, cx ;NOW SI POINTS TO CHR(13).
mov al, 0
mov [ si ], al ;REPLACE CHR(13) BY 0.
;CREATE FILE.
mov ah, 3ch ; dos service to create file
mov cx, 0 ;READ/WRITE MODE.
mov dx, offset filename2 + 2 ;CHARACTERS START AT BYTE 2.
int 21h
jc failed ; end program if failed
mov handle2, ax ; save file handle
;READ ALL BYTES FROM FIRST FILE AND WRITE THEM TO SECOND FILE.
reading:
;READ ONE BYTE.
mov ah, 3FH
mov bx, handle
mov cx, 1 ;HOW MANY BYTES TO READ.
mov dx, offset buf ;THE BYTE WILL BE STORED HERE.
int 21h ;NUMBER OF BYTES READ RETURNS IN AX.
;CHECK EOF (END OF FILE).
cmp ax, 0 ;IF AX == 0 THEN EOF.
je eof
;WRITE BYTE TO THE SECOND FILE.
mov ah, 40h ; write to
mov bx, handle2 ; file
mov dx, offset buf ; where to find data to write
mov cx, 1 ;LENGTH OF STRING IN CX.
int 21h
jmp reading ;REPEAT PROCESS.
eof:
;CLOSE FILES.
mov ah, 3Eh ; close file
mov bx, handle ; which file
int 21h
mov ah, 3Eh ; close file
mov bx, handle2 ; which file
int 21h
failed:
mov ah, 4ch
int 21h
end main
顺便说一句:一次读取一个字节效率不高,实际上,如果文件不是太大,可以一次读取所有文件(效率很高)。另一种选择是两个两个或十个字节读取字节,只处理最后一次读取,因为可能会读取不到 10 个字节。