我要做文件处理操作,但问题是,它只能执行到创建文件。它似乎无法保存要写入文件内的用户输入。我很感激我能得到的每一个帮助,因为我需要今晚完成它。谢谢。
.model small
.stack 100h
.data
buff db 30 ;how many characters
total db 0
char db 30 dup(0) ;for title
com db 100 dup(0) ;for comment
x db "Input your name: ", '$'
handler dw ?
y db "Comment your suggestion, request, feedback etc.: $"
.code
start proc near
mov ax,3
int 10h
mov ax,@data
mov ds,ax
call poop
mov dx,offset buff ;variable para sa input ng name ng file
mov ah,0ah ;BUFFERED INPUT
int 21h
mov bh, 0
mov bl, total
add bx, offset char
mov byte ptr [bx], 0
mov dx, offset char
mov ah, 3ch ;CREATE FILE.
mov cx, 0
int 21h
mov handler, ax
mov ah, 40h ;write string
mov bx, handler
mov cx, 120 ;string length
call poopy
mov dx,offset buff ;variable for input of file
mov ah,0ah ;BUFFERED INPUT
int 21h
mov bh, 0
mov bl, total
add bx, offset com
mov byte ptr [bx], 0
mov dx, offset com ;?
int 21h
mov ah,3eh ;close file
mov bx, handler
int 21h
call exit
exit:
mov ah,4ch
int 21h
start endp
poop proc near
mov dx, offset x
mov ah,9
int 21h
ret
poop endp
poopy proc near
mov dx, offset y
mov ah,9
int 21h
ret
poopy endp
end start
答案 0 :(得分:1)
mov ah, 40h ;write string mov bx, handler mov cx, 120 ;string length
代码中的上述行不合适。还没有什么可以写入新创建的文件。此外,片段不完整,计数器超大(120超过你最终可能拥有的100)。
这是这些代码行的正确位置:
mov dx, offset com ;?
mov ah, 40h ;write string <<<
mov bx, handler <<<
mov cx, 100 <<<
int 21h
mov ah,3eh ;close file
mov bx, handler
int 21h
也许最好只在文件中写入你实际从输入中获得的字节?
然后写
mov dx, offset com
mov ch, 0
mov cl, total
mov bx, handler
mov ah, 40h ;Write file
int 21h
buff db 30 ;how many characters total db 0 char db 30 dup(0) ;for title com db 100 dup(0) ;for comment
您使用DOS缓冲输入两次,但与定义存在冲突。
使用2个单独且完整的定义:
buff1 db 30 ;how many characters
total1 db 0
char db 30 dup(0) ;for title
buff2 db 100 ;how many characters
total2 db 0
com db 100 dup(0) ;for comment
否则重复使用可满足最大需求的单一定义:
buff db 100 ;how many characters
total db 0
buf db 100 dup(0) ;for title or comment
我更喜欢第二种方法,但在询问其他输入之前,请确保将总归零。如果需要较少的输入,您可能还希望减小大小:
mov buff, 30 ;Reduce size for name input
mov total, 0
mov dx, offset buff
mov ah, 0Ah ;BUFFERED INPUT
int 21h
...
mov buff, 100 ;Full size for contents input
mov total, 0
mov dx, offset buff
mov ah, 0Ah ;BUFFERED INPUT
int 21h