Hi
How do you create a text file, save and update a text file, and close a text file in Win32 console?
Much help appreciated.
Thanks
The CreateFile/WriteFile/CloseHandle API functions. CreateFile actually can also simply open a file for reading (don't let the API name confuse you).
I use macros... (attached)
Example use:
.data
filename1 db "file.txt",0
text1 db "hello world",0
.code
main proc
local file1,fname
mov fname,offset filename1
;------------[ these 3 open the same file for reading ]---------[
mov file1,fopen("file1.txt","rb")
;mov file1,fopen(addr filename1,"rb")
;mov file1,fopen(fname,"rb")
;----------------------------------------------------------------/
fclose file1
mov file1,fopen("file1.txt","wb") ; this opens the file for writing
fwrite file1,addr text1,5 ; this writes 5 bytes
fclose file1
ret
main endp
[attachment deleted by admin]
There is not much differences beetwen Win32 console and Win32 GUI applications. You can use file APIs like CreateFile, GetFileSize, WriteFile ... in your win32 console app. You may also use some GUI functions in win32 console app (MessageBox function for example). Here's my code (don't forget to link it with /SUBSYSTEM:CONSOLE switch):
.386
.model small, stdcall
option casemap: none
;========================================================================================================================
include <windows.inc>
include <kernel32.inc>
include <user32.inc>
includelib <kernel32.lib>
includelib <user32.lib>
;========================================================================================================================
.data
g_szFileName db "Test.txt", 0
g_buffer db "This is a test", 0
g_szMsgCaption db "Win32 Console", 0
g_szMsgSuccess db "Success", 0
g_szMsgError db "Error", 0
;========================================================================================================================
.code
main proc
LOCAL hFile: HANDLE
LOCAL dwSize: DWORD
LOCAL dwWritten: DWORD
; Try to create new file (overwrite if exist)
invoke CreateFile, addr g_szFileName, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL
.if eax == INVALID_HANDLE_VALUE
;CreateFile error
invoke MessageBox, NULL, addr g_szMsgError, addr g_szMsgCaption, MB_OK + MB_ICONERROR
ret
.endif
mov hFile, eax ; Save file handle
invoke lstrlen, addr g_buffer
mov dwSize, eax ; Get buffer size
; Try to write buffer to file
invoke WriteFile, hFile, addr g_buffer, dwSize, addr dwWritten, NULL
mov ecx, dwWritten
.if ecx < dwSize
invoke MessageBox, NULL, addr g_szMsgError, addr g_szMsgCaption, MB_OK + MB_ICONERROR
.else
invoke MessageBox, NULL, addr g_szMsgSuccess, addr g_szMsgCaption, MB_OK + MB_ICONINFORMATION
.endif
invoke CloseHandle, hFile ; Close file
ret
main endp
;========================================================================================================================
end main
[attachment deleted by admin]
You can also use the MASM32 write_disk_file procedure (\masm32\m32lib\writdisk.asm) which combines CreateFile (http://msdn2.microsoft.com/en-us/library/aa363858.aspx), WriteFile (http://msdn2.microsoft.com/en-us/library/aa365747(VS.85).aspx), FlushFileBuffers (http://msdn2.microsoft.com/en-us/library/aa364439.aspx), and CloseHandle (http://msdn2.microsoft.com/en-us/library/ms724211.aspx). The MASM32 OutputFile macro wraps the write_disk_file procedure, allowing you to specify the file name as a quoted string. For writing to the same file repeatedly, depending on how many writes are involved, it may be better to separate the operations, creating and/or opening the file and leaving it open until all of the writes (or reads) are complete.
.386
.model flat, stdcall
option casemap :none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\kernel32.lib
TEXT_LEN = 27
.data
text db 'This is a simple text file.'
filename db 'text.txt',0
.data?
hFile dd ?
_size dd ?
.code
start:
invoke CreateFile,ADDR filename,GENERIC_WRITE,\
0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_ARCHIVE,0
mov hFile,eax
invoke WriteFile,eax,ADDR text,TEXT_LEN,ADDR _size,0
invoke CloseHandle,hFile
invoke ExitProcess,0
END start
Thanks everyone,
I understand the processes involved for creating, updating, and closing a text file.
Hi
For reading a file do I just use the following instruction:
invoke ReadFile,FileH,Where,What,addr fread_bytesread,0
I don't understand each parameters?
Can any one explain these parameters?
Also, I am asking "How to read data that is both numbers including integers and floating point numbers as well as text of english words making up sentences from a text file?"
Need help.
Thanks
Egan,
Have a look at the macros in the high level help file under the heading "File IO Macros". I agree that in the longer term you are better to understand the API functions required but these are both fast and easy to use.
it's usually not a good idea to pass eax as a param directly, sometimes the compiler will complain the register will be overwritten and not let you but i've had instances where it didn't complain and it did get overwritten at runtime.