News:

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

Main Menu

Reading numbers from text file

Started by lukasz128, January 02, 2013, 10:26:21 PM

Previous topic - Next topic

lukasz128

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?

qWord

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
MREAL macros - when you need floating point arithmetic while assembling!

lukasz128

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.

Gunther

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
You have to know the facts before you can distort them.

lukasz128


Gunther

Hi lukasz128,

never mind and welcome to the forum.

Gunther
You have to know the facts before you can distort them.

dedndave

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

jj2007

If you don't mind using a library, here is how it can be done:

include \masm32\MasmBasic\MasmBasic.inc        ; download
        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?

Vortex

masm32.lib provides In Memory Text Read and Write functions.

lukasz128

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.

jj2007

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:

lukasz128

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.

jj2007


lukasz128

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 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.

jj2007

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 ;-)