I wanted to create a program which modifies a txt file using WriteFile API

the Last paramter in this API points to OVERLAPPED Structure but the problem is that

i have to change offset from "A" to "Z" and so on then how can i use the Same defined

structure with diffrent offset??
Posted on 2003-04-29 06:08:02 by telophase

Unless its a particuarly big file like a database or similar, often its easier to load it in memory, change what you need and then write it back to disk.

The API SetFilePointer() is used to locate the current position in the file if you want to perfom the modification on disk.

SetEndOfFile() is used to truncate the file if you need to do that.


Posted on 2003-04-29 06:13:51 by hutch--
I already tried using SetFilePointer API but i am not able to set the pointer to correct offset can u give me

some example code which impliments this function??
Posted on 2003-04-29 06:16:57 by telophase
I am not on my development machine but the action is in determining where you start from.

This is in the "dwMoveMethod" parameter which can be,


If you are just working with an offset in the file you would specify FILE_BEGIN and add the file offset to that.

The second parameter "DistanceToMove" is the one where you add your offset from the start.


Posted on 2003-04-29 06:22:50 by hutch--
I could understand that but what about "lpDistanceToMoveHigh"
Posted on 2003-04-29 06:26:16 by telophase
that's for if you want REALLY big files (over 4G) so usually people just set it to 0 (zero)
Posted on 2003-04-29 06:27:27 by Hiroshimator
if you use the board's search feature, and search on the word 'SetFilePointer', you will get this page, and it will keep you busy for a while.

I understand you are a newbie, but you are asking a lot of questions, and you always end up asking for either example code or links to tutorials, so i am showing you the way to get *both*, and you can usually find them quicker than you can type the question. Also, when you post a thread saying a method you tried doesn't work, it can help if you post your code, so that we can point out the errors in *your* code, rather than just giving you a whole fresh bunch of code (which doesn't help you learn).

Hope this helps ;)
Posted on 2003-04-29 06:41:37 by sluggy
hiro, it's more intuitive to use NULL for lpDistanceToMoveHigh since it's a pointer to a dword. Sure, ends up the same in the binary, NULL's just for the tired coder :)
Posted on 2003-04-29 07:59:08 by f0dder
Actually telophase, I just happen to be working on something like that this morning. I am working on a help file merging program for some stuff I'm doing and since cnt files are just text files I have to read and write to them. The following routine reads a file line by line, it's not really efficient but the files I'm dealing with are 10 to 15 lines max so I didn't bother optimizing it. It should show you how to move through a file though.
hFile is a global handle.
ReadFileLines proc uses esi edi ebx

LOCAL buffer[254] :BYTE

lea esi,Char
lea edi,buffer
xor ebx,ebx

invoke ReadFile,hFile,esi,1,ADDR BytesRead,NULL
cmp BytesRead,NULL
je @F
mov al,[esi]
cmp al,0Dh
je @F
mov [edi],al
inc edi
inc ebx
jmp ReadLine
mov [edi],BYTE PTR 0
lea edi,buffer
; [color=red]This is what you want I think[/color]
[b]invoke SetFilePointer,hFile,1,NULL,FILE_CURRENT[/b]

; [color=red]The next line is where I process the single line from the text file[/color]
; [color=red]ebx contains the length of the line in chars[/color]
invoke DecodeLine,ADDR buffer,ebx

cmp BytesRead,NULL
je @F
xor ebx,ebx
jmp ReadLine

ReadFileLines endp
PS: your message box is full or I would have replied to your PM :)
Posted on 2003-04-29 11:40:44 by donkey