I can't get a whole idea how to do it. My program has 2 parameters which are row numbers and the rest are text files (no matter how many). Let's say
program.exe 2 4 sample.txt sample1.txt
Explanation: 2nd row of sample.txt and sample1.txt is changed to 4 row
sample.txt contains this text:
hello my name is
i like dogs
the sun is shining
fast cars are awesome
sample1.txt contains this text:
Stackoverflow is awesome
The treat is yours
I like turtles
The train is fast
After execution, our txt files look like this:
sample.txt
hello my name is
fast cars are awesome
the sun is shining
fast cars are awesome
sample1.txt
Stackoverflow is awesome
The train is fast
I like turtles
The train is fast
The question is: How do I open the file and make changes to it and then save it. And how to use buffers in this situation?
I believe i have to make a counter, which will start reading that line to buffer when it reaches the value of the 1st parameter.
Then, I will make 2nd counter, which will start reading that line to buffer when it reaches the value of the 2st parameter.
And how do I use the same file? I have no idea. Because i have 2 buffers. What will happen to other lines? Are they going to dissapear?
Sorry that I can't explain everything the way you understand.
This should get you started, as it shows how to open and read a file in DOS asm. You have to add your logic of course. Keep in mind that you can read more than one character into the buffer, but reading one by one makes it a bit simpler to process lines, because you can read until you reach the end of the file or the end of a line, and then do whatever you need to do with it.
If you want to modify the same file as the input, then you have to keep in mind, that you either have to fully read the file into memory and then rewrite it from scratch. This would work if the files are small enough to fit completely into memory. If you need to process arbitrary big files, then you would have to open a temporary file, write to it whatever is needed, and then rename it to the original file. In case of an error this will at least not destroy the original file.
And you really should look up on google for DOS int
list. There are many docs out there, so you know what you need to call.
.model small
.data
Filename db 'test.txt',0
FHndl dw ?
Buffer db 80h dup(?)
.stack 100h
.code
Program:
; Setup the data segment
mov ax,@data
mov ds,ax
mov ah, 3dh ; Open the file
mov al, 0 ; Open for reading
lea dx, Filename
int 21h
; jc BadOpen
mov FHndl, ax ; Remeber file handle
ReadByte:
mov ah,3fh ; Read data from the file
lea dx, Buffer ; Address of data buffer
mov cx, 1 ; Read one (or more bytes into the buffer)
mov bx, FHndl
int 21h
; jc ReadError
cmp ax, cx ; Did we successfully read all characters?
jne EOF
; ... here goes your code to process the characters.
jmp ReadByte ; Read next byte
EOF:
mov bx, FHndl
mov ah, 3eh ; Close file
int 21h
;jc CloseError
retn
End Program