Yo Guys, i want a data added in a text file by ady be create and also some previous data in the text file. The tutorial that i found are just replace the front word/data.
伙计们,我想要一个由阿迪在文本文件中添加的数据,并在文本文件中创建一些以前的数据。我找到的教程只是替换了前面的单词/数据。
i want to add a new data in the text file that the new data is after the previous data. After the program , the new data must at the last line of the text file and the previous data are still there.
我想在文本文件中添加一个新数据,即新数据在以前的数据之后。程序完成后,新数据必须位于文本文件的最后一行,以前的数据仍在那里。
My idea is to use int 21h,42h to hold the buffer when reading the file and int 21h ,40h to write the data in text file. But i still not idea how to do. Is it use those both function ? If yes then how to do it ?
我的想法是在读取文件时使用INT 21h,42h来保存缓冲区,使用INT 21h,40h来将数据写入文本文件。但我还是不知道该怎么做。它使用的是这两种功能吗?如果是,那么怎么做呢?
Example :
示例:
Before (test.txt):
在(est.txt)之前:
Banana$
香蕉$
After Program (if i want add "Banana" in):
节目后(如果我想在中添加“Banana”):
BananaBanana$
香蕉香蕉$
Thanks and hope u guys have a great day !
谢谢你们,祝你们有愉快的一天!
更多回答
Service 42h, int 21h
moves file pointer inside a file. So move file pointer to the end of a file and append new data. ah = 42h
, mov al,2
, mov cx,0
, mov dx,0
, int 21h
.
服务42h,int 21h在文件内移动文件指针。因此,将文件指针移动到文件末尾,并追加新数据。Ah=42h,mov al,2,mov Cx,0,mov dx,0,int 21h。
So, I had a little bit fun today with this assignment. ;) I applied few file services int 21h
:
所以,我今天的任务很有趣。;)我在21小时内应用了很少的文件服务:
5bh
- Create file for reading or writing
3dh
- Open file
42h
- Move file pointer to calculate file size
40h
- Write to file and screen
3eh
- Close file
5BH-创建用于读取或写入的文件3DH-打开文件42H-移动文件指针以计算文件大小40H-写入文件和屏幕3EH-关闭文件
.286
.model small
.stack 0ffh
.data
file_name db 'file.txt',0 ; The name of the file
inputSize db 11 ; buffered input, ah = 0ah, int 21h, size 10 characters + enter
inputChars db 0 ; how many characters user inputed
inputBuffer db 11 dup(0) ; space for characters
buffer db 30 dup(0) ; read from file and write data here
buffer_size dw 30 ; append up to 30 bytes
memory_left dw 30 ; do we have more space for data?
file_handle dw 0
file_size dw 0
array_4_value db 5 dup('x')
file_status_msg db "File status: ",'$'
before db "Data inside file: ",13,10,'$'
file_error db "<< Something is wrong with the file (ax = 2,3,4 or 5).",13,10,13,10,'$'
file_exists db "<< File already exists (ax = 80) >>",13,10,13,10,'$'
file_success db "<< File successfuly created >>",13,10,13,10,'$'
file_open_error db "<< Error opening the file (ax = 2,3,4,5,12) >>",13,10,13,10,'$'
hdd_error_msg db "Not enough memory. Sorry, can't append new data.",13,10,13,10,"Press any key to quit.",'$'
free_mem db "Free memory : ",'$'
bytes db " byte(s) left.",13,10,13,10,'$'
line_30 db "------------------------------",13,10,'$'
data_added db 13,10,"Data added successfuly. ",13,10,13,10,'$'
append_msg db "Add text to file (max 10 characters): ",'$'
new_line db 13,10,'$'
.code
main:
mov ax,@data
mov ds,ax
;;---------------------------------------
;; Create the file for reading/writing
mov ah,09h ; Print message "File status: "
lea dx, file_status_msg
int 21h
create_file:
mov ah, 5bh
lea dx, file_name ; file name asciiz
mov cx,0 ; normal attributes
int 21h
jc create_error ; error ?
mov ah,09h ; Print message "<< File successfuly created >>"
lea dx, file_success
int 21h
;;---------------------------------------
;; Open the file for reading/writing
open_file:
mov ah, 3dh
lea dx, file_name ; file name asciiz
mov al,2 ; read/write permission
int 21h
jc open_error ; error ?
;;---------------------------------------
;; Save the file handle
handle:
mov [file_handle], ax
;;---------------------------------------
;; Calculate file size
calculate_file_size:
mov ah,42h ; move file pointer
mov bx, [file_handle] ; file handle
mov al,2 ; move file pointer starting from end of the file
mov cx,0 ; cx:dx = new pointer location, beginning of the file
mov dx,0
int 21h
;;---------------------------------------
;; Save file size
mov [file_size], ax ; number of characters in file
;;---------------------------------------
;; Reset file pointer to start of the file
reset_and_read:
mov ah,42h ; move file pointer
mov bx, [file_handle] ; file handle
mov al,0 ; file pointer was at the end of the file now should be at the beginning
mov cx,0 ; cx:dx = new pointer location, beginning of the file
mov dx,0
int 21h
;;---------------------------------------
;; Print file content
mov ah,09h ; Print message "Data inside file before modification: "
lea dx, before
int 21h
mov ah,09h ; Print message "------"
lea dx, line_30
int 21h
read_service_3fh:
mov ah,3fh ; read file or device
mov cx, [buffer_size] ; how many bytes to read
mov bx, [file_handle] ; file handle
lea dx,buffer ; where to read
int 21h
print_to_screen_service_40h:
mov ah,40h ; write file or device
mov cx, [buffer_size] ; how many bytes to write
mov bx, 1 ; write to screen
lea dx,buffer ; what to write
int 21h
mov ah,09h ; move to new line
lea dx, new_line
int 21h
mov ah,09h ; Print message "------"
lea dx, line_30
int 21h
mov ah,09h ; move to new line
lea dx, new_line
int 21h
;;---------------------------------------
;; Print how many more bytes we can add
mov ah,09h ; Print message "Free memory : "
lea dx, free_mem
int 21h
mov ax,[file_size]
mov bx,[buffer_size]
sub bx,ax
mov [memory_left],bx
mov ax,bx
push ax
;;---------------------------------------------
;; This is probably worst part in the code :D
;; Just calculate how long is value and print digits
mov cl, 5 ; max value is 65535 so we need 5 characters
mov ch, 0
mov bl, 10
mov di,offset array_4_value + 4
bin_2_ascii:
div bl
cmp al, 0
jz b2a_show
mov [di],ah
dec di
mov ah, 0
dec cl
jnz bin_2_ascii
b2a_show:
mov [di],ah
mov si, offset array_4_value
mov cx, 5
b2a_print:
mov dl,[si]
cmp dl,'x'
jz skip
mov ah,2
add dl,30h
int 21h
skip:
inc si
dec cx
jnz b2a_print
mov ah,09h ; Print message " byte(s)"
lea dx, bytes
int 21h
pop ax
cmp ax,0
jz mem_error
;;---------------------------------------
;; Add new data to file
mov ah,09h ; Print message "Add text to file (max 10 characters): "
lea dx, append_msg
int 21h
mov ah,0ah ; user input
lea dx, inputSize
int 21h
disk_space:
mov ax,[file_size]
add al,[inputChars]
cmp ax,30
jg mem_error
append_to_file_service_40h:
mov ah,40h ; write file or device
xor cx,cx
mov cl, [inputChars] ; how many bytes to write
mov bx, [file_handle] ; write to file
lea dx,inputBuffer ; what to write
int 21h
mov ah,09h ; move to new line
lea dx, new_line
int 21h
mov ah,09h ; Print message "Data dded."
lea dx, data_added
int 21h
mov di, offset array_4_value
mov cx, 5
mov al, 'x'
array_clear:
mov [di], al
inc di
loop array_clear
jmp calculate_file_size
jmp_hlt:
hlt
jmp jmp_hlt
mem_error:
mov ah,09h ; move to new line
lea dx, hdd_error_msg
int 21h
mov ah,8
int 21h
jmp closeApp
mov ah,40h ; reading file or device
mov bx,1
mov al,0 ; move file pointer starting from begining of the file
mov cx,[file_size] ; cx:dx, where to move pointer
mov dx,0 ; file_size, number of characters in file
int 21h
create_error:
cmp ax,80
jz file_already_exists
mov ah,09h ; Print message "<< Something is wrong with the file (ax = 2,3,4 or 5)>>"
lea dx, file_error
int 21h
jmp file_msg_done
file_already_exists:
mov ah,09h ; Print message "<< File already exists (ax = 80) >>"
lea dx, file_exists
int 21h
file_msg_done:
jmp open_file
open_error:
mov ah,09h ; Print message "<< Error opening the file (ax = 2,3,4,5,12) >>"
lea dx, file_open_error
int 21h
closeApp:
mov ah, 3eh
mov bx,[file_handle]
int 21h
mov ax, 4C00h ; exit
int 21h
end main
更多回答
Nassau Bro, which school or college are u from ?
拿骚兄弟,你是哪所学校或大学的?
I finished schools long time ago. Why? I did something wrong?
我很久以前就毕业了。为什么?我做错了什么吗?
ohh okkk noting hahaha
Ohh OKKK注意哈哈哈
我是一名优秀的程序员,十分优秀!