The MASM Forum Archive 2004 to 2012

General Forums => The Workshop => Topic started by: white scorpion on August 16, 2005, 02:07:37 PM

Title: Reading a file one line at a time
Post by: white scorpion on August 16, 2005, 02:07:37 PM
Hi all,

It's been a while since i've been here since i'm awfully busy the last couple of months, but now i have another question to ask:

I'm writing a program which needs to read from a ini file one line at the time.
I know i am able to read the file until the newline character is reached but i was actually hoping for a function that already has that ability since i need to make the code as easy to understand for others as possible. Especially for people with no coding experience it is hard to understand such a function.

Anyone knows of such a function i can invoke which is already part of the windows API's or part of the masm library?

Thanks in advance!

Kind regards,

Mark
Title: Re: Reading a file one line at a time
Post by: hutch-- on August 16, 2005, 02:14:20 PM
This procedure in the masm32 library is designed for that purpose.

readline proc source:DWORD,buffer:DWORD,spos:DWORD


Have a look at the example in the service pack called GETINI, it uses the macro for the same procedure "linein$" and it shows you how to use it.
Title: Re: Reading a file one line at a time
Post by: white scorpion on August 16, 2005, 08:43:51 PM
Thanks hutch!

Btw, i just had to re-download masm32 after a reinstall and i saw that you haven't got a mirror in the Netherlands yet. Let me know if you are interested then i will put it on my site as well.

Regards,

Mark
Title: Re: Reading a file one line at a time
Post by: Vortex on August 17, 2005, 08:00:09 AM
Here is a quick example :

.386
.model flat,stdcall
option casemap:none

include \masm32\include\windows.inc
include \masm32\include\masm32.inc
include \masm32\include\kernel32.inc

includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\masm32.lib

.data
spos    dd  0
source  db 'This is line 1 , ',13,10,'This one is line 2 , '
        db 'Finally, this is line 3',13,10
.data?
buffer  db  100 dup(?)
.code
start:
    invoke  readline,ADDR source,ADDR buffer,spos
    or      eax,eax
    jz      @f
    mov     spos,eax
    invoke  StdOut,ADDR buffer
    jmp     start
@@:   
    invoke  ExitProcess,0
END start
Title: Re: Reading a file one line at a time
Post by: ToutEnMasm on August 17, 2005, 04:17:22 PM
Hello,
You can also have a look on my site,The source of my library (coming with the download of editmasm) contain proc usable to load a textfile,call the lines of the textfiles by there numbers, in any order that you want and with saving time of scrutation.
                                               ToutEnMasm
Title: Re: Reading a file one line at a time
Post by: white scorpion on August 19, 2005, 05:14:36 AM
Thanks guys, but the following lines already work like they should:


mov hFile, InputFile("inifile.ini")
mov rpos,0

my_loop:
mov rpos,linein$(hFile,ADDR buffer,rpos)
lea eax,buffer
cmp byte ptr [eax],'#'
jz my_loop
invoke lstrlen,addr buffer
.IF eax==0
    jmp next
.ENDIF
invoke MessageBoxA,NULL,addr buffer,addr buffer,MB_OK   
jmp my_loop
next:
invoke CloseHandle,hFile
invoke ExitProcess,0

If the first character of a line contains a # then it is seen as comment and not used.