Author Topic: Reading numbers from text file  (Read 8820 times)

lukasz128

  • Guest
Reading numbers from text file
« 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?

qWord

  • Member
  • *****
  • Posts: 1473
  • The base type of a type is the type itself
    • SmplMath macros
Re: Reading numbers from text file
« Reply #1 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
MREAL macros - when you need floating point arithmetic while assembling!

lukasz128

  • Guest
Re: Reading numbers from text file
« Reply #2 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.

Gunther

  • Member
  • *****
  • Posts: 3585
  • Forgive your enemies, but never forget their names
Re: Reading numbers from text file
« Reply #3 on: January 02, 2013, 11:13:48 PM »
Hi 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.

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

Gunther
Get your facts first, and then you can distort them.

lukasz128

  • Guest
Re: Reading numbers from text file
« Reply #4 on: January 02, 2013, 11:31:06 PM »
Thanks for your help.

Gunther

  • Member
  • *****
  • Posts: 3585
  • Forgive your enemies, but never forget their names
Re: Reading numbers from text file
« Reply #5 on: January 02, 2013, 11:53:04 PM »
Hi lukasz128,

never mind and welcome to the forum.

Gunther
Get your facts first, and then you can distort them.

dedndave

  • Member
  • *****
  • Posts: 8823
  • Still using Abacus 2.0
    • DednDave
Re: Reading numbers from text file
« Reply #6 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

jj2007

  • Member
  • *****
  • Posts: 9686
  • Assembler is fun ;-)
    • MasmBasic
Re: Reading numbers from text file
« Reply #7 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
        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

  • Member
  • *****
  • Posts: 1994
Re: Reading numbers from text file
« Reply #8 on: January 03, 2013, 05:24:47 AM »
masm32.lib provides In Memory Text Read and Write functions.

lukasz128

  • Guest
Re: Reading numbers from text file
« Reply #9 on: January 05, 2013, 03:56:01 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

  • Member
  • *****
  • Posts: 9686
  • Assembler is fun ;-)
    • MasmBasic
Re: Reading numbers from text file
« Reply #10 on: January 05, 2013, 04:42:23 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

  • Guest
Re: Reading numbers from text file
« Reply #11 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.

jj2007

  • Member
  • *****
  • Posts: 9686
  • Assembler is fun ;-)
    • MasmBasic
Re: Reading numbers from text file
« Reply #12 on: January 05, 2013, 07:42:38 AM »
What is better to use HeapAlloc or GlobalAlloc?

Check yourself ;-)

lukasz128

  • Guest
Re: Reading numbers from text file
« Reply #13 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 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

  • Member
  • *****
  • Posts: 9686
  • Assembler is fun ;-)
    • MasmBasic
Re: Reading numbers from text file
« Reply #14 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 ;-)