The MASM Forum

General => The Campus => Topic started by: lukasz128 on January 02, 2013, 10:26:21 PM

Title: Reading numbers from text file
Post by: lukasz128 on January 02, 2013, 10:26:21 PM
I have a text file with numbers, each one in different line. Also I have a proc that "changes" read string to number. My question is how I can read text from file. With numbers in text file looking like these:
10 15 8 1 2
I read byte after byte and I can distinguish that I have full number read with simple compare to space. But I don't know how to do it when numbers are in different lines. How can I do this?
Title: Re: Reading numbers from text file
Post by: qWord on January 02, 2013, 10:38:06 PM
That depends on the format of your text file. For Windows/DOS text files there are commonly two bytes for a line break: 13, 10 (CR,LF). For the Linux/Unix world only LF is used.
If you are not sure, scan for both, 13,10 and 10 only.

http://en.wikipedia.org/wiki/Newline
Title: Re: Reading numbers from text file
Post by: lukasz128 on January 02, 2013, 10:44:33 PM
I'm sure that it will be windows text files... So I simply have to compare just like before with space but in this case two bytes one after another containing 13 and 10.
Title: Re: Reading numbers from text file
Post by: Gunther on January 02, 2013, 11:13:48 PM
Hi lukasz128,

Quote from: lukasz128 on January 02, 2013, 10:44:33 PM
I'm sure that it will be windows text files... So I simply have to compare just like before with space but in this case two bytes one after another containing 13 and 10.

yes, if it is a DOS/Windows based text file.

Gunther
Title: Re: Reading numbers from text file
Post by: lukasz128 on January 02, 2013, 11:31:06 PM
Thanks for your help.
Title: Re: Reading numbers from text file
Post by: Gunther on January 02, 2013, 11:53:04 PM
Hi lukasz128,

never mind and welcome to the forum.

Gunther
Title: Re: Reading numbers from text file
Post by: dedndave on January 03, 2013, 02:33:14 AM
you don't really care what the file bytes are - space, carriage return, line feed, commas, etc
if it isn't an ASCII numeric char (30h to 39h), then it's a delimiter
Title: Re: Reading numbers from text file
Post by: jj2007 on January 03, 2013, 04:12:25 AM
If you don't mind using a library, here is how it can be done:

include \masm32\MasmBasic\MasmBasic.inc        ; download (http://masm32.com/board/index.php?topic=94.0)
        Init
        Let esi=FileRead$("MyNumbers.txt")        ; read the whole file into a buffer
        Open "O", #1, "MyNumbers.bin"        ; open for Output
        xor ecx, ecx        ; zero a counter
        .While 1
                void Val(esi)        ; convert from ASCII to DWORD in eax
                .Break .if edx==-127        ; edx will be -127 for invalid numbers, e.g. at EOF
                add esi, edx        ; otherwise edx contains #bytes used
                push eax        ; create a buffer and fill it with eax ;-)
                mov eax, esp        ; get the address of the buffer
                PrintBuffer #1, eax, DWORD        ; pass it to the print routine, and tell it to write a dword
                pop eax        ; correct the stack
                inc ecx        ; increment the counter
        .Endw
        Close #1        ; close the output file
        Inkey Str$("%i numbers written", ecx)        ; and give some feedback
        Exit
end start

Source attached (asc=rtf, Wordpad or RichMasm)

P.S.: If your input numbers are bytes, and you need binary output as bytes, modify accordingly, i.e.
PrintBuffer #1, eax, BYTE
Val swallows almost every kind of input, but I guess you want DWORDs, right?
Title: Re: Reading numbers from text file
Post by: Vortex on January 03, 2013, 05:24:47 AM
masm32.lib provides In Memory Text Read and Write functions.
Title: Re: Reading numbers from text file
Post by: lukasz128 on January 05, 2013, 03:56:01 AM
Quote from: jj2007 on January 03, 2013, 04:12:25 AM
If you don't mind using a library, here is how it can be done:

I do mind using library. I'd like to write it on my own and it's part of my semestral project so I want to write it in similar way as we were doing it on classes.
Title: Re: Reading numbers from text file
Post by: jj2007 on January 05, 2013, 04:42:23 AM
Quote from: lukasz128 on January 05, 2013, 03:56:01 AM
I do mind using library. I'd like to write it on my own and it's part of my semestral project

That was not clear from your posts. Rolling your own is always a good exercise :t
You are using OllyDbg? You will need it. Plus some functions from the big Windows library, such as
HeapAlloc, HeapFree
CreateFile
ReadFile

Don't hesitate to come back once you have code to show :icon14:
Title: Re: Reading numbers from text file
Post by: lukasz128 on January 05, 2013, 07:35:35 AM
In matter a fact I used it. I had to implement heapsort algorithm and when I had trouble Ollydbg was very usefull. What is better to use HeapAlloc or GlobalAlloc? In other of my tasks I have to dynamically alocate memory for dword variables and I'm looking for good way of doing this.
Title: Re: Reading numbers from text file
Post by: jj2007 on January 05, 2013, 07:42:38 AM
Quote from: lukasz128 on January 05, 2013, 07:35:35 AMWhat is better to use HeapAlloc or GlobalAlloc?

Check yourself (http://masm32.com/board/index.php?topic=64.msg190#msg190) ;-)
Title: Re: Reading numbers from text file
Post by: lukasz128 on January 05, 2013, 08:02:05 AM
I've seen this thread a few minutes ago... And I think I should use HeapAlloc. According to this: http://msdn.microsoft.com/en-us/library/windows/desktop/aa366597%28v=vs.85%29.aspx (http://msdn.microsoft.com/en-us/library/windows/desktop/aa366597%28v=vs.85%29.aspx) I have to get a handle, which function I should use: HeapCreate or GetProcessHeap. To my simple mind I think I should use the second one but I'm not entirely sure. This is my last question on this matter if I have more I'll create a separate topic for them.
Title: Re: Reading numbers from text file
Post by: jj2007 on January 05, 2013, 08:07:03 AM
Use GetProcessHeap, and don't create new threads. This thread is yours, use it for all questions - some believe it needs a new thread for every burp but it just creates confusion ;-)
Title: Re: Reading numbers from text file
Post by: MichaelW on January 05, 2013, 08:32:23 AM
Quote from: lukasz128 on January 05, 2013, 07:35:35 AM
What is better to use HeapAlloc or GlobalAlloc?

For what you are doing I think the best choice is the one that is easier to use, and the same for the functions to open and read the file.
Title: Re: Reading numbers from text file
Post by: lukasz128 on January 05, 2013, 08:45:20 AM
So new question. I have a struct of three dword elements, I have a block of memory size of this struct and for example variable first of this struct. What I need is to replace my "first" by new one.The solution I see is:

invoke HeapAlloc, heapHandle, HEAP_ZERO_MEMORY, sizeof Element
mov pointer, eax
lea ebx, first
mov [ebx], [pointer]
mov [ebx + 4], [pointer + 4]
mov [ebx + 8]. [pointer + 8]

Is there a easier way of doing this? I know it won't work but I only wanted to show sketch. And for question why I need it for:  I'm implementing doubly linkedlist and I need it to "insert" method.
Title: Re: Reading numbers from text file
Post by: FORTRANS on January 05, 2013, 09:36:05 AM
Hi,

   Look at the MOVe String command.  REP MOVSD will simplify
the transfer, but it requires more setup and the index registers.

Regards,

Steve N.
Title: Re: Reading numbers from text file
Post by: qWord on January 05, 2013, 10:14:42 AM
Use structures for the linked list (more readable). e.g.:
NODE struct
flink PVOID ?
blink PVOID ?
data DWORD 4 dup (?)
NODE ends

mov ebx, first ; first = ptr NODE

invoke HeapAlloc,...
mov edx,[ebx].NODE.flink
mov [ebx].NODE.flink,eax
mov [eax].NODE.blink,ebx
mov [eax].NODE.flink,edx
.if edx
mov [edx].NODE.blink,eax
.endif
mov [eax].NODE.data[0],...
mov [eax].NODE.data[4],...
Title: Re: Reading numbers from text file
Post by: lukasz128 on January 07, 2013, 08:10:00 AM
Quote from: qWord on January 05, 2013, 10:14:42 AM
Use structures for the linked list (more readable). e.g.:
It worked quite well, thanks.
Title: Re: Reading numbers from text file
Post by: Gunther on January 07, 2013, 08:17:12 AM
Hi lukasz128,

Quote from: lukasz128 on January 07, 2013, 08:10:00 AM
Quote from: qWord on January 05, 2013, 10:14:42 AM
Use structures for the linked list (more readable). e.g.:
It worked quite well, thanks.

that's was friends are for.

Gunther