Information on String Conversions

File IO

There are only some many things that you can do with files. These file operations are common to all languages and operating systems.

The information on how to use these is in MASM32 High Level Reference, File IO Macros.

File IO Macros

Load and Save Complete File

InputFileRead entire file into a memory buffer
OutputFileWrite a memory buffer to disk

Low Level File Operations

fcreateCreate a new file and return its handle
fdeleteDelete a disk file
fopen Open an existing file and return its handle
fcloseClose an open file handle
fread Read data from an open file
fwriteWrite data to an open file
fprintWrite line of text to an open file
fprintcWrite text to open file with C style formatting
fseekSet the current position in the open file
fseteofSet the end of the open file to the current location
fsizeReturn the size in bytes of the open file

A Sample Look At The Documentation

fcreate

mov hFile, fcreate(filename)

Description

Create a new file with read / write access.

Parameters

1. filename A zero terminated string. This can be a quoted text.

Return Value

The return value is the handle of the new file.

Comments

The new file has read and write access. If there is an error, you should test the return value with GetLastError().

Example

mov hFile, fcreate("mynewfile.ext")

Something Is Missing

The return value is returned in EAX, so hFile has to be declared as a DWORD.

Another Sample Look At The Documentation


fprintc

fprintc hFile,"Hi Guys\n"

Description

Write zero terminated text with C style formatting to an open file.

Parameters

1. hFile The handle of the open file 2. lpText The address of a zero terminated string. This may also be quoted text that contains C style escape sequences as provided for in the cfm$ macro.

Return Value

This macro does not return a value.

Comments

This macro allows the programmer to write text to an open file using the text formatting capacity of the cfm$ macro which allows C style formatting of string data. It is useful where you need to sequentially append text to a file without necessarily placing it on a new line.

cfm$ Macro

This is from the MASM32 High Level Reference, Normal String Macros.

cfm$

Description

Format a quoted string using C style escape sequences.

Parameters

A quoted string that can have escape sequences contained within it.

Return Value

The return value is the address of the formated string data as an offset in the .DATA section.

Comments

This macro is not designed to conform to the C "printf" string formatting specification, it is designed to give this form of formatting convenience to MASM programmers who may have a C background. It can be used either directly in a procedure call in normal code or be used in a dedicated macro that requires this style of formatting. The direct formatting escapes are as follows, the numbers enclosed in brackets are the ASCII characters inserted into the quoted text. \n = newline (13,10) \t = tab (9) \\ = "\" backslash (92) \q = quote (34) The following escapes are MASM specific to bypass the restrictions that MASM places on certain characters being contained within a quoted string. \l = "<" left angle bracket. (60) \r = ">" right angle bracket. (62) \x = "!" exclaimation mark. (33) \a = "(" left bracket. (40) \b = ")" right bracket. (41)

Example

print cfm$("\tThis is a test\n\tof the \qfmt$\q macro\n")

Result

This is a test of the "fmt$" macro

Sample Program

 
comment * =========================================
            Information on StrToFloat and
            FloatToStr are in the MASM32 Library
            Reference, Floating Point Conversions
            ======================================= *
            
; ллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
    include \masm32\include\masm32rt.inc
; ллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл

comment * -----------------------------------------------------
                        Build this  template with
                       "CONSOLE ASSEMBLE AND LINK"
        ----------------------------------------------------- *

    .code

start:
   
; ллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл

    call main
    inkey
    exit

; ллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл

main proc
    LOCAL hFile :DWORD                          ; file handle
    LOCAL bwrt  :DWORD                          ; variable for bytes written
    LOCAL txt   :DWORD                          ; text handle

    cls
    sas txt,"Gary says 'Hi!'"                   ; assign string to local variable
    mov hFile, fcreate("testfile.txt")          ; create the file
    mov bwrt, fwrite(hFile,txt,len(txt))        ; write data to it 
	
    ; How much got written out?
    mov eax, [bwrt]
    print str$(eax)
    print " bytes were written to the file"
    print chr$(10,13)
	
    fclose  hFile  					; It is good programming style to close a file as soon as possible!
    ret

main endp

; ллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл

end start

Results On The Screen

15 bytes were written to the file
Press any key to continue ...

Results In The File

Gary says 'Hi!'