News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

SetFilePointer problem

Started by minor28, August 13, 2012, 06:24:09 PM

Previous topic - Next topic

minor28

I am trying to read a text file from the end and backwards.
The last two characters are 0Dh 0Ah.

lea edi,Buffer
@@:
invoke SetFilePointer,hFile,-1,0,FILE_CURRENT
invoke ReadFile,hFile,addr char,1,addr charRead,0
lea eax,char
mov al,byte ptr [eax]
.if al==0Ah || al==0Dh
nop
.endif
mov byte ptr [edi],al
inc edi
jmp @B


This only reads the last 0Ah character without moving one
character back.


lea edi,Buffer
@@:
invoke SetFilePointer,hFile,-1,0,FILE_END
invoke ReadFile,hFile,addr char,1,addr charRead,0
lea eax,char
mov al,byte ptr [eax]
.if al==0Ah || al==0Dh
nop
.endif
mov byte ptr [edi],al
inc edi
invoke SetEndOfFile,hFile
jmp @B


This also repeatedly reads the last 0Ah.

How do I read backward?

dedndave

let me start by saying....
if i find myself "going against nature" like that, it usually means i need to rethink the code design
you might be better off to:
1. get the file size
2. subtract the buffer size from the file size
3. set the file pointer to that value
4. read to fill the buffer
5. work on the data backwards   :P

but, if you must.....
generally, i try to avoid using any starting point with SetFilePonter other than the beginning of the file
so:
1. get the file size
2. subtract 1
3. set the pointer to that value, relative to the beginning of the file
4. read 1 byte
5. repeat steps 2 through 4

that sounds very inefficient, though

minor28

Thanks for your answer.

I am writing to the file and want to go back to a point to isert some text.

This works.


lea edi,Buffer
mov newFp,0
@@:
dec newFp
invoke SetFilePointer,hIncFile,newFp,0,FILE_END
invoke ReadFile,hIncFile,addr char,1,addr charRead,0
lea eax,char
mov al,byte ptr [eax]
.if al==0Ah || al==0Dh
nop
.endif
mov byte ptr [edi],al
inc edi
jmp @B
@@:
invoke SetEndOfFile,hIncFile
;start writing again


But I still don't know why the first code doesn't word.

dedndave

invoke SetFilePointer,hFile,-1,0,FILE_CURRENT

when you read, it adjusts the pointer to the end of the read location
so, you set the pointer back, read, which moves it forward, then move it back one   :P

you could try -2, but the first pass has to be modified somehow

invoke SetFilePointer,hFile,-1,0,FILE_END

this code always sets the pointer to the same spot, unless the file size changes

minor28

Yes, of course it is. I should have thought of it is a step backward and one step forward.

dedndave

there are reasons for setting relative to the beginning of the file

let's say you have 2 areas of code that access the same file
if you use end-relative or current-relative pointers, you are asking for problems
end-relative can get messy if one section changes the size of the file

i just use beginning-relative ("absolute") pointers and it seems to make life simple   :P

Zen

MINOR 28,
What I usually do is write alot of ridiculous CODE BLOAT, to cover every conceivable set of circumstances when reading a file.
For instance,...you could easily do a preliminary scan of the file to determine:   

  • (1) If the file is the correct file and format.
  • (2) A character search, to see that the data you are anticipating actually exists in the file.
This is just an example, of course,...but, it eliminates the two most common errors you typically encounter when reading a file.
...By doing this, you will know if the data at the end of the file (where you want to make the insert) is configured like you expect, and, the exact location that the SetFilePointer will read or write.

...Otherwise, (if this doesn't make sense to you)...I am almost always WRONG,...
...And,...DAVE,...is almost always RIGHT,...