Read from and write to file question.

Hello there.

I'm working on a little program to read in one byte at a time, alter the byte, then write it back.

Whenever currently when I read in a byte and write it back, the byte written is actually written in the byte after the one read.

Ex.
Data in file:
444
After adding 1 to the bytes and writing them back this is what I have.
45455

I'm using the follwing code for reading and writing.

READ
mov bx, HANDLE ; bx contains the file handle
mov dx, offset BUFFER ; move the address of the buffer to dx
mov ah, 3Fh ; function for read in from file
mov cx, 1 ; cx designates to read in 1 byte
int 21h

WRITE
mov cx, 1 ; write one byte
mov ah, 40h ; write to file
int 21h

Is there a register that holds the place in the file of the next byte to be read/written in so that I could decremement it after reading in?

Thanks for your time.
[968 byte] By [Alklun] at [2007-11-20 7:44:07]
# 1 Re: Read from and write to file question.
Because whenever you write into file, it writes at location where file pointer is. You should create new temp file to write all your altered data, when done delete the read file and rename the temp file.

BTW, instead of invoking int 21h, you can very well call Win32 APIs i.e. ReadFille, WriteFile, SetFilePointer etc.
Krishnaa at 2007-11-10 3:55:11 >
# 2 Re: Read from and write to file question.
Hi Alklun,

There's no special register keeping the current place in file, but service 42h of int 21h retrieves/relocates the pointer of any file open in your program.

You do this by indicating the file pointer in BX and the file segment-offset in CX and DX. There are several websites documenting the 42h function.

Thus, the steps would be:
1. Open file in input-output mode (ax=3d02h).
2. Read a byte into the one-byte buffer.
3. If (CF=1 or AL=0h)
then go to Step 8.
4. Increase content of the one-byte buffer.
5. Decrease by 1 the file pointer (ax=4201h and bx, cx, dx as necessary).
6. Write modified content into file.
7. Go to Step 2.
8. Close file or do further processing.

Another solution is to open file twice, so you would have two different, independent file pointers. Read using one pointer, write using the other one. Here's the complete code (assembled with tasm) ... sorry for the lack of tags ... and comments.

main segment
assume cs:main,ds:main,es:main,ss:main
org 100h

start:jmp begin
usrfile db 'file.txt',00h
hand1 dw 00h
hand2 dw 00h
content db 00h

begin: mov ax,3d00h
mov dx,offset usrfile
int 21h
jc the_end
mov hand1,ax
mov ax,3d01h
mov dx,offset usrfile
int 21h
mov hand2,ax

rd_wrt: mov ah,3fh
mov bx,hand1
mov cx,1
mov dx,offset content
int 21h
mov di,offset content
mov al,[di]
inc al
cld
stosb
mov ah,40h
mov bx,hand2
mov cx,1
mov dx,offset content
int 21h

close: mov ah,3eh
mov bx,hand2
int 21h
mov ah,3eh
mov bx,hand1
int 21h
the_end: mov ax,4c00h
int 21h
main ends
end start

Good luck

Iaki Viggers
iviggers at 2007-11-10 3:56:11 >
# 3 Re: Read from and write to file question.
Ah, that's great.

Thank you both.
Alklun at 2007-11-10 3:57:22 >