0

the problem focuses on MASM and using Irvine 32 bits only. I don't plan to use macro or etc unless really needed.

.386     
.model flat, stdcall  
INCLUDE Irvine32.inc
.stack 4096     
      
.Data
 inputFilename        byte "Enter filename : ",0
 errorMssg            byte  "Unable to open file !",0    
 newAmount            byte "2021",0                         ;updated from dword to byte
 fileHandle           HANDLE ?
 buffer               dword   200 DUP(?)
 
 .Code
 main PROC 

I would like to prompt user enter a filename. Then Open the file if it exist, if not error message is display. Lets assume the file is created and therefore exist. In the file contains just a number which is 2020.

 OPEN_EXISTING_FILE:

        mov edx,offset inputFilename        ;ask user enter the existing filename
                                            ;currently in existing file content is 2020       
        call WriteString
        call  Crlf
        mov ecx, SIZEOF inputFilename
        call ReadString
        call OpenInputFile
        mov fileHandle,eax
        
        ;file error check
        cmp eax, INVALID_HANDLE_VALUE                 
        jne OVERWRITE_FILE                   
        mov edx, offset errorMssg
        call WriteString
        call  Crlf
        jmp OPEN_EXISTING_FILE 

It seems my Write to File is not working. Am i missing some codes? Can anyone guide me on how can i replace the "2020" to "2021", in the file.

  OVERWRITE_FILE:

        mov eax,fileHandle 
        mov edx, OFFSET newAmount             ;i want to overwrite 2020 to 2021 in the existing file
        mov ecx, LENGTHOF newAmount   
        call WriteToFile
        call CloseFile
        call crlf
        call waitMsg
        
END main  
Skyb
  • 11
  • 5
  • `WriteToFile` expects a pointer in`edx` and a length in `ecx`. Consult the Irvine documentation or the source. Also, if your file has the value as text, you should write it as text not as a binary number that you have right now. – Jester Mar 26 '21 at 12:46
  • @Jester Hi Jester, tq for reply! i have added `edx` and length in `ecx`. But it still does not work.... Also, what do you mean by binary number? My file is storing text (2020 as integer). Technically I would like to perform calculation (Eg: calc pricing) then store in newAmount variable. Then I would like to retrieve it from the variable and overwrite into my existing file. Sorry if my explanation is confusing, do you get what i mean? otherwise I'll try to explain again .. – Skyb Mar 26 '21 at 14:47
  • 1
    If your file has text, e.g. you can open it in notepad and see the `2020` in it, then you want `newAmount byte "2021"` – Jester Mar 26 '21 at 15:34
  • @Jester i have changed it to byte.. but it still does not work (Still not being able to overwrite value in Notepad). I also looked into irvine documentation, it seems my code looks fine though? But hmm i've been working on it for a long time and I still cant solve it :( – Skyb Mar 27 '21 at 17:36
  • 1
    `OpenInputFile` is specifically for reading. Try `CreateOutputFile` instead. – Jester Mar 27 '21 at 17:58
  • 1
    @Jester Oh ! 'CreateOutputFile' works!!. Thank you Jester! I think i had the wrong concept, i thought since i am opening an existing file so i used 'OpenInputFile' – Skyb Mar 27 '21 at 18:18

0 Answers0