使用DOS系统调用附加到文本文件 [英] Appending to a text file with DOS system calls
问题描述
美好的一天!我这里有一个TASM代码,当我再次运行该程序时,它不会追加新的字符串.追加的代码行是什么?非常感谢!
Good day! I have here a TASM code and it does not append the new string when I run the program again. What are the lines of code for appending? Thank you very much!
; This example program creates a file and then writes to it.
.model small
.stack
.data
CR equ 13
LF equ 10
StartMessage DB "This program creates a file called NEW.TXT"
DB ,"on the C drive.$"
EndMessage DB CR,LF,"File create OK, look at file to"
DB ,"be sure.$"
WriteMessage DB "An error has occurred (WRITING)$"
OpenMessage DB "An error has occurred (OPENING)$"
CreateMessage DB "An error has occurred (CREATING)$"
WriteMe DB "HELLO, THIS IS A TEST, HAS IT WORKED?",0
FileName DB "new.txt",0 ; name of file to open
Handle DW ? ; to store file handle
.code
START:
mov ax,@data ; base address of data segment
mov ds,ax ; put it in ds
mov dx,offset StartMessage
mov ah,09h
int 21h
mov dx,offset FileName ; put offset of filename in dx
xor cx,cx ; clear cx - make ordinary file
mov ah,3Ch ; function 3Ch - create a file
int 21h ; call DOS service
jc CreateError ; jump if there is an error
mov dx,offset FileName ; put offset of filename in dx
mov al,2 ; access mode -read and write
mov ah,3Dh ; function 3Dh - open the file
int 21h ; call dos service
jc OpenError ; jump if there is an error
mov Handle,ax ; save value of handle
mov dx,offset WriteMe ; address of information to write
mov bx,Handle ; file handle for file
mov cx,38 ; 38 bytes to be written
mov ah,40h ; function 40h - write to file
int 21h ; call dos service
jc WriteError ; jump if there is an error
cmp ax,cx ; was all the data written?
jne WriteError ; no it wasn't - error!
mov bx,Handle ; put file handle in bx
mov ah,3Eh ; function 3Eh - close a file
int 21h ; call dos service
mov dx,offset EndMessage
mov ah,09h
int 21h
ReturnToDOS:
mov ax,4C00h ; terminate program
int 21h
WriteError:
mov dx,offset WriteMessage
jmp EndError
OpenError:
mov dx,offset OpenMessage
jmp EndError
CreateError:
mov dx,offset CreateMessage
EndError:
mov ah,09h
int 21h
mov ax,4C01h
int 21h
END START
此外,在将字符串放入文件之前,是否需要终止程序?非常感谢您!
Also, does the program need to be terminated before the string can be put into the file? Thank you very much!
编辑
@ us2012:按照您的回答,我应该替换
EDIT
@us2012: Following your answer, should I replace
mov bx,Handle ; file handle for file
mov cx,38 ; 38 bytes to be written
mov ah,40h ; function 40h - write to file
int 21h ; call dos service
使用
mov dx,offset WriteMe ; address of information to write
mov bx,Handle ; file handle for file
mov cx,38 ; 38 bytes to be written
;xor dx, dx
mov ah,40h ; function 40h - write to file
mov al, 02h
int 21h ; call dos service
?当我再次运行该程序时,它仍然没有追加.仍然是单个HELLO, THIS IS A TEST, HAS IT WORKED?
.
? It still doesn't append when I run the program again. Still a single HELLO, THIS IS A TEST, HAS IT WORKED?
.
推荐答案
int21h参考文献指出,要移动文件指针,可以使用函数42h
.详细信息在此处,因此以下内容应适用于在写入之前查找文件的末尾:
int21h reference says that to move the file pointer, you'd use function 42h
. Details are here, so the following should work for seeking to the end of the file before writing:
mov bx, Handle
xor cx, cx
xor dx, dx
mov ah, 42h
mov al, 02h
int 21h
这篇关于使用DOS系统调用附加到文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!