News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Please help me to save file with MASM32

Started by elmo, October 19, 2010, 05:44:16 AM

Previous topic - Next topic

elmo

I found example in
\masm32\examples\exampl02\qikpad  to save a file.



Write_2_Disk proc lpszFile_Name:DWORD
    LOCAL ln    :DWORD
    LOCAL hMem$ :DWORD
    LOCAL hFile :DWORD
    LOCAL bw    :DWORD
    LOCAL txtBuffer[64]

  ; -----------------------------------------
  ; truncate file to zero length if it exists
  ; -----------------------------------------
    invoke CreateFile,lpszFile_Name,    ; pointer to name of the file
            GENERIC_WRITE,              ; access (read-write) mode
            NULL,                       ; share mode
            NULL,                       ; pointer to security attributes
            CREATE_ALWAYS,              ; how to create
            FILE_ATTRIBUTE_NORMAL,      ; file attributes
            NULL

    mov hFile,eax

    invoke GetWindowTextLength,hEdit
    mov ln, eax
    inc ln

    invoke SysAllocStringByteLen,0,ln
    mov hMem$, eax

    invoke GetWindowText,hEdit,hMem$,ln

    invoke WriteFile,hFile,hMem$,ln,ADDR bw,NULL

    invoke SysFreeString,hMem$
    invoke CloseHandle,hFile

    invoke SendMessage,hEdit,EM_SETMODIFY,FALSE,0

    invoke lnstr,ADDR szSavedAt
    inc eax
    invoke MemCopy,ADDR szSavedAt,ADDR txtBuffer,eax

    invoke dwtoa,ln,ADDR sizeBuffer
    invoke lstrcat,ADDR txtBuffer,ADDR sizeBuffer
    invoke lstrcat,ADDR txtBuffer,ADDR bytes

    invoke SendMessage,hStatus,SB_SETTEXT,2,ADDR txtBuffer

    ret
Write_2_Disk endp



It can save a file.
Example:
I want to save a file with name FileName.asm in the direktocy C:\
But when I want to save a file with the same name in the same directory, it will overwrite that FileName.asm without prevent us.
Can we show Message like "FileName already exist". Then if we, click no, it will prevent us from that overwrite' action?
Could you help me to solve this problem? Thank you.

Fritz Gamaliel
be the king of accounting programmer world!

jj2007

Check if it exists, and act accordingly.

Quotefilesize


filesize proc lpszFileName:DWORD


Description

This function returns the length of a file if it exists.


Parameter

1. lpszFileName The zero terminated string that has the path & name of the file.


Return Value

If the return value is minus one (-1) then the file does not exist, otherwise eax returns the size of the file in bytes.

elmo

If the return value is minus one (-1) then the file does not exist, otherwise eax returns the size of the file in bytes

hi jj2007,
Do you mean the code below:

; -----------------------------------------
; truncate file to zero length if it exists
; -----------------------------------------
invoke CreateFile,
ADDR szFileName[0],   ;pointer to name of the file
GENERIC_WRITE,      ; access (read-write) mode
NULL,         ; share mode
NULL,         ; pointer to security attributes
CREATE_ALWAYS,      ; how to create
FILE_ATTRIBUTE_NORMAL,   ; file attributes
NULL

mov hFile,eax



it always show eax= the size of the file in bytes.
So, it's still overwrite the existing file.
Could you help me to solve this problem?Thank you
Fritz
be the king of accounting programmer world!

jj2007

Check with filesize if the file exists, then act accordingly. You are lucky that I am not Bogdan ::)

elmo

thank you .
The problem solved :U :clap: :bg
be the king of accounting programmer world!