News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Read the data from a file and print the content of it

Started by ToanBao122, October 17, 2020, 05:01:33 PM

Previous topic - Next topic

ToanBao122

Hello everyone, say if I'm to have a "text.txt" file that contains a simple string "Hello,world". Can you show me a program to read this file and then print out its contents? This is what I was trying to do albeit unsuccessfully. Thank you!
INCLUDE Irvine32.inc
BUFMAX = 128
.data
buffer   BYTE   BUFMAX+1 DUP(0)
bufSize  DWORD  ?
fileName BYTE "textt.txt",0
fileHandle HANDLE ?
bytesRead dword ?
.code
main PROC
mov  EDX,OFFSET fileName
call OpenInputFile
mov  fileHandle, EAX
mov  eax,fileHandle
mov  edx,OFFSET buffer
mov  ecx,BUFMAX
call ReadFromFile
jc   show_error_message
mov  bytesRead,eax
mov  edx, offset bytesRead
call WriteString
exit
main ENDP



jj2007

I don't know much about Irvine's strange library (nobody uses it here), but if you hand over OFFSET buffer to ReadFile, could it be that WriteString might also want to be informed about this buffer?

Check also what Irvine offers for debugging registers. It might happen that your ReadFile fails miserably because it doesn't find textt.txt on your disk.

ToanBao122

The "textt.txt" was a minor spelling mistake on my part, however, it wasn't exactly the case. Can you elaborate on this part :" if you hand over OFFSET buffer to ReadFile, could it be that WriteString might also want to be informed about this buffer?"  Sorry, I'm new to MASM in general.

jj2007


Vortex

Hi ToanBao122,

This is not an example based on Irvine's include files but it can give you some ideas :

include     ReadFile.inc

.data

FileName    db 'test.txt',0     ; file to read

.data?

hFile       dd ?
FileSize    dd ?
hMem        dd ?
BytesRead   dd ?

.code

start:

    call    main
    invoke  ExitProcess,0

main PROC   uses ebx

    xor     ebx,ebx
   
    invoke  CreateFile,ADDR FileName,GENERIC_READ,ebx,ebx,\
            OPEN_EXISTING,ebx,ebx           

    mov     hFile,eax

    invoke  GetFileSize,eax,ebx

    mov     FileSize,eax
    inc     eax

    invoke  GlobalAlloc,GMEM_FIXED,eax
    mov     hMem,eax

    add     eax,FileSize

    mov     BYTE PTR [eax],bl  ; Set the last byte to NULL so that StdOut
                               ; can safely display the text in memory.

    invoke  ReadFile,hFile,hMem,FileSize,\
            ADDR BytesRead,ebx

    invoke  CloseHandle,hFile

    invoke  StdOut,hMem

    invoke  GlobalFree,hMem
    ret

main ENDP


END start

ToanBao122

@jj2007 It says the WriteString function takes in EDX as a string pointer as argument.

ToanBao122

Anyway thanks everyone for the inpute, I've figureed out the problem is that the content of the string is not in the bytesRead but in the buffer, and in the line before call WriteString, I should set "mov edx, offset buffer" instead of "mov edx, offset bytesRead". Have a good day everyone!

jj2007


ToanBao122

Anyone know of a way that I could get the length of the string in the buffer?

hutch--

Pass the ADDRESS of the string to the following procedure. Return value is in the EAX register.

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE

slen proc ptxt:DWORD

    mov eax, [esp+4]
    sub eax, 1

  lbl0:
    add eax, 1
    cmp BYTE PTR [eax], 0
    jne lbl0

    sub eax, [esp+4]

    ret 4

slen endp

OPTION PROLOGUE:PrologueDef
OPTION EPILOGUE:EpilogueDef

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

Vortex

Hi ToanBao122,

You also have the API function lstrlen

QuoteDetermines the length of the specified string (not including the terminating null character).

https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-lstrlena