flat assembler
Message board for the users of flat assembler.

Index > Windows > WriteFile

Author
Thread Post new topic Reply to topic
ly



Joined: 11 Oct 2005
Posts: 13
ly
Hi
I wrote a program that create a new file and then writes some text to it.
(CreateFile and WriteFile.)
if I open again the file for writing more text it deletes the prev text and write the new one instead.
How can i append text to already exist written file ? (without deleting the prev text)
Some code examples please...
thanks
ly
Post 01 Oct 2006, 15:27
View user's profile Send private message Reply with quote
UCM



Joined: 25 Feb 2005
Posts: 285
Location: Canada
UCM
You will have to open the file with OPEN_EXISTING, then use SetFilePointer to set the file pointer to the end of the file so you can append data.
Post 01 Oct 2006, 15:44
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
Or use OPEN_ALWAYS so the file will be created automatically if it's the first time you use it.

http://windowssdk.msdn.microsoft.com/en-us/library/ms685006.aspx
Post 01 Oct 2006, 16:39
View user's profile Send private message Reply with quote
ly



Joined: 11 Oct 2005
Posts: 13
ly
Hello UCM
thanks
I tried but it doesn't work.
can you write sample code for SetFilePointer ?
thanks ly
Post 01 Oct 2006, 16:42
View user's profile Send private message Reply with quote
ly



Joined: 11 Oct 2005
Posts: 13
ly
Hello UCM
Its OK now it works.(I use OPEN_ALWAYS)
thanks again
Post 01 Oct 2006, 16:50
View user's profile Send private message Reply with quote
barmentalisk



Joined: 06 Sep 2005
Posts: 36
barmentalisk
Trouble with write file.
This code works correctly:
Code:
format PE GUI 4.0
entry start

include 'win32a.inc'


section '.data' data readable writeable

        fname     db 'E:\test.txt',0
        hfile     dd ?
        pmem      db '0123456789AB'
        lpNumberOfBytesWritten dd ?

section '.code' code readable executable

  start:
        invoke  CreateFile,fname,GENERIC_READ + GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0
        mov     [hfile],eax
        invoke  WriteFile,[hfile],pmem,12,lpNumberOfBytesWritten,0
        invoke  CloseHandle,[hfile]
        invoke  ExitProcess,0



section '.idata' import data readable writeable

  library kernel32,'KERNEL32.DLL'

  include 'api\kernel32.inc'    


But this one ... :
Code:
format PE GUI 4.0
entry start

include 'win32a.inc'


section '.data' data readable writeable

        fname     db 'E:\test.txt',0
        hfile     dd ?
        hheap     dd ?
        pmem      dd ?
        lpNumberOfBytesWritten dd ?

section '.code' code readable executable

  start:
        invoke  CreateFile,fname,GENERIC_READ + GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0
        mov     [hfile],eax
        invoke  GetProcessHeap
        mov     [hheap],eax
        invoke  HeapAlloc,[hheap],HEAP_ZERO_MEMORY,12
        mov     [pmem],eax
        mov     [pmem],'0123'
        mov     [pmem+4],'4567'
        mov     [pmem+8],'89AB'
        invoke  WriteFile,[hfile],pmem,12,lpNumberOfBytesWritten,0
        invoke  HeapFree,[hheap],0,pmem
        invoke  CloseHandle,[hfile]
        invoke  ExitProcess,0



section '.idata' import data readable writeable

  library kernel32,'KERNEL32.DLL'

  include 'api\kernel32.inc'
    

It doesn't save to file '4567'. No matter what method I use to put text to memory and no matter how long is the text, - it always have missed second dword of data in the resulting file.
Post 20 Jun 2008, 10:32
View user's profile Send private message Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1901
DOS386
Code:
        invoke  HeapAlloc,[hheap],HEAP_ZERO_MEMORY,12
        mov     [pmem],eax     ; Why ???
        mov     [pmem],'0123'  ; Overwrite ???
        mov     [pmem+4],'4567'
        mov     [pmem+8],'89AB' 
    


Faulty indexing Sad

> always miss the '4567'

Because it gets overwritten by the lp...blah...blah...blah Wink

_________________
Bug Nr.: 12345

Title: Hello World program compiles to 100 KB !!!

Status: Closed: NOT a Bug
Post 20 Jun 2008, 11:27
View user's profile Send private message Reply with quote
shoorick



Joined: 25 Feb 2005
Posts: 1605
Location: Ukraine
shoorick
try this
Code:
        invoke  HeapAlloc,[hheap],HEAP_ZERO_MEMORY,12 
        mov     [pmem],eax 
        mov     dword [eax],'0123' 
        mov     dword [eax+4],'4567' 
        mov     dword [eax+8],'89AB' 
        invoke  WriteFile,[hfile],eax,12,lpNumberOfBytesWritten,0 
        invoke  HeapFree,[hheap],0,[pmem]
    

_________________
UNICODE forever!
Post 20 Jun 2008, 11:42
View user's profile Send private message Visit poster's website Reply with quote
barmentalisk



Joined: 06 Sep 2005
Posts: 36
barmentalisk
Sorry, may be I need some sleep =)))
thank you very much !!!
Post 20 Jun 2008, 12:09
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar.

Powered by rwasa.